home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 51 / Amiga Format CD51 (2000-03-10)(Future Publishing)(GB)[!][issue 2000-04].iso / -in_the_mag- / workbench / term_4.8 / extras / source / term-source.lha / Init.c < prev    next >
C/C++ Source or Header  |  1997-10-13  |  100KB  |  4,538 lines

  1. /*
  2. **    Init.c
  3. **
  4. **    Program initialization and shutdown routines
  5. **
  6. **    Copyright © 1990-1997 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. **
  9. **    :ts=4
  10. */
  11.  
  12. #ifndef _GLOBAL_H
  13. #include "Global.h"
  14. #endif
  15.  
  16.     /* This will later go into the screen and window title */
  17.  
  18. #ifdef CPU_ANY
  19. STATIC STRPTR Machine = "";
  20. #else
  21. STATIC STRPTR Machine = "'020+ ";
  22. #endif
  23.  
  24.     /* The default public screen, if it is locked */
  25.  
  26. STATIC struct Screen *DefaultPubScreen;
  27.  
  28.     /* This variable helps us to remember whether the fast!
  29.      * macro panel was open or not.
  30.      */
  31.  
  32. STATIC BOOL HadFastMacros = FALSE;
  33.  
  34.     /* Remember whether we did pen allocation or not. */
  35.  
  36. STATIC BOOL AllocatedPens = FALSE;
  37.  
  38.     /* ChooseMasking(struct Screen * Screen):
  39.      *
  40.      *    Choose whether or not masked drawing operations
  41.      *    should be used for that screen.
  42.      */
  43.  
  44. STATIC BOOL
  45. ChooseMasking(struct Screen * Screen)
  46. {
  47.     BOOL CanUseMasking = FALSE;
  48.  
  49.     if(GetBitMapDepth(Screen->RastPort.BitMap) > 1)
  50.     {
  51.         if(Kick30)
  52.         {
  53.             if(!(GetBitMapAttr(Screen->RastPort.BitMap,BMA_FLAGS) & BMF_INTERLEAVED))
  54.                 CanUseMasking = TRUE;
  55.         }
  56.         else
  57.         {
  58.             CanUseMasking = TRUE;
  59.         }
  60.     }
  61.  
  62.     return(CanUseMasking);
  63. }
  64.  
  65.     /* SimpleRefreshAllowed(struct Configuration *Config):
  66.      *
  67.      *    Checks whether the terminal window should be and can
  68.      *    be a simple refresh window.
  69.      */
  70.  
  71. STATIC BOOL
  72. SimpleRefreshAllowed(struct Configuration *Config)
  73. {
  74.     BOOL SimpleRefresh;
  75.  
  76.     if((Config->ScreenConfig->UseSimpleRefresh) &&
  77.        (XEmulatorBase == NULL || Config->TerminalConfig->EmulationMode != EMULATION_EXTERNAL))
  78.     {
  79.         SimpleRefresh = TRUE;
  80.     }
  81.     else
  82.     {
  83.         SimpleRefresh = FALSE;
  84.     }
  85.  
  86.     return(SimpleRefresh);
  87. }
  88.  
  89.     /* AddExtraAssignment(STRPTR LocalDir,STRPTR Assign):
  90.      *
  91.      *    Add assignments for local directories.
  92.      */
  93.  
  94. STATIC VOID
  95. AddExtraAssignment(STRPTR LocalDir,STRPTR Assign)
  96. {
  97.     UBYTE LocalBuffer[MAX_FILENAME_LENGTH];
  98.     BPTR FileLock;
  99.  
  100.         /* Add the colon, we'll need it later */
  101.  
  102.     LimitedSPrintf(sizeof(LocalBuffer),LocalBuffer,"%s:",Assign);
  103.  
  104.         /* Is the local directory present? */
  105.  
  106.     if(FileLock = Lock(LocalDir,ACCESS_READ))
  107.     {
  108.             /* Is the assignment present? */
  109.  
  110.         if(IsAssign(LocalBuffer))
  111.         {
  112.                 /* Check to see if the local directory */
  113.                 /* is already on the assignment list */
  114.  
  115.             if(LockInAssign(FileLock,LocalBuffer))
  116.             {
  117.                 UnLock(FileLock);
  118.  
  119.                 FileLock = NULL;
  120.             }
  121.         }
  122.     }
  123.  
  124.         /* Can we attach the lock to the assignment list? */
  125.  
  126.     if(FileLock)
  127.     {
  128.         Forbid();
  129.  
  130.             /* If the assignment is already present, add the */
  131.             /* new directory, else create a new assignment. */
  132.  
  133.         if(IsAssign(LocalBuffer))
  134.             AssignAdd(Assign,FileLock);
  135.         else
  136.             AssignLock(Assign,FileLock);
  137.  
  138.         Permit();
  139.     }
  140. }
  141.  
  142.     /* StatusSizeSetup():
  143.      *
  144.      *    Precalculates the size of the status line display.
  145.      */
  146.  
  147. STATIC VOID
  148. StatusSizeSetup(struct Screen *Screen,LONG *StatusWidth,LONG *StatusHeight)
  149. {
  150.     SZ_SizeSetup(Screen,(struct TextAttr *)&UserFont);
  151.  
  152.     if(Config->TerminalConfig->EmulationMode == EMULATION_EXTERNAL && Config->TerminalConfig->EmulationFileName[0] && Config->ScreenConfig->StatusLine != STATUSLINE_DISABLED && !Config->ScreenConfig->SplitStatus)
  153.     {
  154.         *StatusHeight = 0;
  155.  
  156.         return;
  157.     }
  158.  
  159.     if(Config->ScreenConfig->StatusLine != STATUSLINE_DISABLED)
  160.     {
  161.         if(Config->ScreenConfig->StatusLine == STATUSLINE_COMPRESSED)
  162.         {
  163.             *StatusWidth    = 80 * UserFontWidth;
  164.             *StatusHeight    = UserFontHeight;
  165.  
  166.             if(Config->ScreenConfig->SplitStatus)
  167.             {
  168.                 *StatusWidth    += 2;
  169.                 *StatusHeight    += 2;
  170.             }
  171.         }
  172.         else
  173.         {
  174.             LONG ColumnLeft[4],ColumnWidth[4];
  175.             LONG i,Len,Max;
  176.  
  177.             *StatusWidth    = 0;
  178.             *StatusHeight    = SZ_BoxHeight(2);
  179.  
  180.             ColumnLeft[0] = SZ_LeftOffsetN(MSG_TERMSTATUSDISPLAY_STATUS_TXT,MSG_TERMSTATUSDISPLAY_FONT_TXT,-1);
  181.             ColumnLeft[1] = SZ_LeftOffsetN(MSG_TERMSTATUSDISPLAY_PROTOCOL_TXT,MSG_TERMSTATUSDISPLAY_TERMINAL_TXT,-1);
  182.             ColumnLeft[2] = SZ_LeftOffsetN(MSG_TERMSTATUSDISPLAY_BAUDRATE_TXT,MSG_TERMSTATUSDISPLAY_PARAMETERS_TXT,-1);
  183.             ColumnLeft[3] = SZ_LeftOffsetN(MSG_TERMSTATUSDISPLAY_TIME_TXT,MSG_TERMSTATUSDISPLAY_ONLINE_TXT,-1);
  184.  
  185.             Max = 0;
  186.  
  187.             for(i = MSG_TERMAUX_READY_TXT ; i <= MSG_TERMAUX_HANG_UP_TXT ; i++)
  188.             {
  189.                 if((Len = SZ_BoxWidth(strlen(LocaleString(i)))) > Max)
  190.                     Max = Len;
  191.             }
  192.  
  193.             for(i = MSG_TERMSTATUSDISPLAY_FROZEN_TXT ; i <= MSG_TERMSTATUSDISPLAY_RECORDING_TXT ; i++)
  194.             {
  195.                 if((Len = SZ_BoxWidth(strlen(LocaleString(i)))) > Max)
  196.                     Max = Len;
  197.             }
  198.  
  199.             ColumnWidth[0] = Max;
  200.  
  201.             Max = SZ_BoxWidth(12);
  202.  
  203.             for(i = MSG_TERMAUX_ANSI_VT102_TXT ; i <= MSG_TERMAUX_HEX_TXT ; i++)
  204.             {
  205.                 if((Len = SZ_BoxWidth(strlen(LocaleString(i)))) > Max)
  206.                     Max = Len;
  207.             }
  208.  
  209.             ColumnWidth[1] = Max;
  210.  
  211.             Max = SZ_BoxWidth(10);
  212.  
  213.             for(i = MSG_TERMAUX_NONE_TXT ; i <= MSG_TERMAUX_SPACE_TXT ; i++)
  214.             {
  215.                 if((Len = SZ_BoxWidth(4 + strlen(LocaleString(i)))) > Max)
  216.                     Max = Len;
  217.             }
  218.  
  219.             ColumnWidth[2] = Max;
  220.  
  221.             ColumnWidth[3] = SZ_BoxWidth(8);
  222.  
  223.             for(i = 0 ; i < 4 ; i++)
  224.                 *StatusWidth += ColumnWidth[i] + ColumnLeft[i];
  225.  
  226.             *StatusWidth += 4 + 3 * InterWidth;
  227.  
  228.             if(!Config->ScreenConfig->SplitStatus)
  229.                 *StatusHeight += 4;
  230.             else
  231.                 *StatusHeight += 2;
  232.         }
  233.     }
  234.     else
  235.         *StatusHeight = 0;
  236. }
  237.  
  238.     /* LoadKeyMap(STRPTR Name):
  239.      *
  240.      *    Load a keymap file from disk.
  241.      */
  242.  
  243. STATIC struct KeyMap *
  244. LoadKeyMap(STRPTR Name)
  245. {
  246.     struct KeyMapResource *KeyMapResource;
  247.     struct KeyMap *Map = NULL;
  248.  
  249.         /* Try to get access to the list of currently loaded
  250.          * keymap files.
  251.          */
  252.  
  253.     if(KeyMapResource = (struct KeyMapResource *)OpenResource("keymap.resource"))
  254.     {
  255.         struct KeyMapNode *Node;
  256.  
  257.             /* Try to find the keymap in the list. */
  258.  
  259.         Forbid();
  260.  
  261.         if(Node = (struct KeyMapNode *)FindName(&KeyMapResource->kr_List,FilePart(Name)))
  262.             Map = &Node->kn_KeyMap;
  263.  
  264.         Permit();
  265.     }
  266.  
  267.         /* Still no keymap available? */
  268.  
  269.     if(!Map)
  270.     {
  271.         APTR OldPtr;
  272.  
  273.             /* Disable DOS requesters. */
  274.  
  275.         DisableDOSRequesters(&OldPtr);
  276.  
  277.             /* Unload the old keymap code. */
  278.  
  279.         if(KeySegment)
  280.             UnLoadSeg(KeySegment);
  281.  
  282.             /* Try to load the keymap from the
  283.              * name the user entered.
  284.              */
  285.  
  286.         if(!(KeySegment = LoadSeg(Name)))
  287.         {
  288.             UBYTE LocalBuffer[MAX_FILENAME_LENGTH];
  289.  
  290.                 /* Second try: load it from
  291.                   * the standard keymaps drawer.
  292.                   */
  293.  
  294.             strcpy(LocalBuffer,"KEYMAPS:");
  295.  
  296.             if(AddPart(LocalBuffer,FilePart(Name),sizeof(LocalBuffer)))
  297.             {
  298.                 if(!(KeySegment = LoadSeg(LocalBuffer)))
  299.                 {
  300.                     strcpy(LocalBuffer,"Devs:Keymaps");
  301.  
  302.                     if(AddPart(LocalBuffer,FilePart(Name),sizeof(LocalBuffer)))
  303.                         KeySegment = LoadSeg(LocalBuffer);
  304.                 }
  305.             }
  306.         }
  307.  
  308.             /* Did we get the keymap file? */
  309.  
  310.         if(KeySegment)
  311.         {
  312.             struct KeyMapNode *Node = (struct KeyMapNode *)&((ULONG *)BADDR(KeySegment))[1];
  313.  
  314.             Map = &Node->kn_KeyMap;
  315.         }
  316.  
  317.             /* Enable DOS requesters again. */
  318.  
  319.         EnableDOSRequesters(OldPtr);
  320.     }
  321.     else
  322.     {
  323.         if(KeySegment)
  324.         {
  325.             UnLoadSeg(KeySegment);
  326.  
  327.             KeySegment = NULL;
  328.         }
  329.     }
  330.  
  331.     return(Map);
  332. }
  333.  
  334.     /* DeleteOffsetTables(VOID):
  335.      *
  336.      *    Delete the line multiplication tables.
  337.      */
  338.  
  339. STATIC VOID
  340. DeleteOffsetTables(VOID)
  341. {
  342.     FreeVecPooled(OffsetXTable);
  343.     OffsetXTable = NULL;
  344.  
  345.     FreeVecPooled(OffsetYTable);
  346.     OffsetYTable = NULL;
  347. }
  348.  
  349.     /* CreateOffsetTables(VOID):
  350.      *
  351.      *    Allocate the line multiplication tables.
  352.      */
  353.  
  354. STATIC BOOL
  355. CreateOffsetTables(VOID)
  356. {
  357.     LONG Width,Height;
  358.  
  359.     Width    = (Window->WScreen->Width  + TextFontWidth)  * 2 / TextFontWidth;
  360.     Height    = (Window->WScreen->Height + TextFontHeight) * 2 / TextFontHeight;
  361.  
  362.     DeleteOffsetTables();
  363.  
  364.     if(OffsetXTable = (LONG *)AllocVecPooled(Width * sizeof(LONG),MEMF_ANY))
  365.     {
  366.         if(OffsetYTable = (LONG *)AllocVecPooled(Height * sizeof(LONG),MEMF_ANY))
  367.         {
  368.             LONG i,j;
  369.  
  370.             for(i = j = 0 ; i < Width ; i++, j += TextFontWidth)
  371.                 OffsetXTable[i] = j;
  372.  
  373.             for(i = j = 0 ; i < Height ; i++, j += TextFontHeight)
  374.                 OffsetYTable[i] = j;
  375.  
  376.             return(TRUE);
  377.         }
  378.     }
  379.  
  380.     DeleteOffsetTables();
  381.  
  382.     return(FALSE);
  383. }
  384.  
  385.     /* ReleaseDefaultPubScreen():
  386.      *
  387.      *    Unlocks the default public screen in case it had been locked.
  388.      */
  389.  
  390. STATIC VOID
  391. ReleaseDefaultPubScreen(VOID)
  392. {
  393.     if(DefaultPubScreen)
  394.     {
  395.         UnlockPubScreen(NULL,DefaultPubScreen);
  396.         DefaultPubScreen = NULL;
  397.     }
  398. }
  399.  
  400.     /* SafeOpenLibrary(STRPTR Name,LONG Version):
  401.      *
  402.      *    Try to open a library, but if there already is
  403.      *    a version in memory that's older than the release
  404.      *    we want flush it out first.
  405.      */
  406.  
  407. struct Library *
  408. SafeOpenLibrary(STRPTR Name,LONG Version)
  409. {
  410.     struct Library *Base;
  411.  
  412.     Forbid();
  413.  
  414.         /* Is this library already in memory? */
  415.  
  416.     if(Base = (struct Library *)FindName(&SysBase->LibList,FilePart(Name)))
  417.     {
  418.             /* An old release? */
  419.  
  420.         if(Base->lib_Version < Version)
  421.         {
  422.                 /* Flush it out. */
  423.  
  424.             RemLibrary(Base);
  425.         }
  426.     }
  427.  
  428.     Permit();
  429.  
  430.         /* Now reopen the library. */
  431.  
  432.     return(OpenLibrary(Name,Version));
  433. }
  434.  
  435.     /* TTYResize():
  436.      *
  437.      *    Signal AmigaUW that the window size has changed.
  438.      */
  439.  
  440. VOID
  441. TTYResize()
  442. {
  443.     if(Window && WriteRequest)
  444.     {
  445.         LONG Columns,Lines;
  446.  
  447.         if(XEmulatorBase && XEM_IO)
  448.         {
  449.             if(XEmulatorBase->lib_Version < 4)
  450.                 Columns = Lines = 0;
  451.             else
  452.             {
  453.                 ULONG Result = XEmulatorInfo(XEM_IO,XEMI_CONSOLE_DIMENSIONS);
  454.  
  455.                 Columns    = XEMI_EXTRACT_COLUMNS(Result);
  456.                 Lines    = XEMI_EXTRACT_LINES(Result);
  457.             }
  458.         }
  459.         else
  460.         {
  461.             Columns    = LastColumn + 1;
  462.             Lines    = LastLine + 1;
  463.         }
  464.  
  465.         if(Columns > 0 && Lines > 0)
  466.         {
  467.             WriteRequest->IOSer.io_Command    = UWCMD_TTYRESIZE;
  468.             WriteRequest->IOSer.io_Data        = (APTR)((Columns << 16) | (Lines));
  469.             WriteRequest->IOSer.io_Length    = (WindowWidth << 16) | (WindowHeight);
  470.  
  471.             DoIO((struct IORequest *)WriteRequest);
  472.         }
  473.     }
  474. }
  475.  
  476.     /* UpdateTerminalLimits():
  477.      *
  478.      *    Check the current window size and extract the
  479.      *    size and position of the usable window rectangle.
  480.      */
  481.  
  482. VOID
  483. UpdateTerminalLimits()
  484. {
  485.     WindowLeft        = Window->BorderLeft;
  486.     WindowTop        = Window->BorderTop;
  487.  
  488.     WindowWidth        = Window->Width - (Window->BorderLeft + Window->BorderRight);
  489.     WindowHeight    = Window->Height - (Window->BorderTop + Window->BorderBottom);
  490.  
  491.     if(StatusWindow)
  492.     {
  493.         StatusDisplayLeft    = StatusWindow->BorderLeft;
  494.         StatusDisplayTop    = StatusWindow->BorderTop;
  495.         StatusDisplayWidth    = StatusWindow->Width - (StatusWindow->BorderLeft + StatusWindow->BorderRight);
  496.         StatusDisplayHeight    = StatusWindow->Height - (StatusWindow->BorderTop + StatusWindow->BorderBottom);
  497.     }
  498.     else
  499.     {
  500.         if(Config->ScreenConfig->StatusLine != STATUSLINE_DISABLED && !(Config->TerminalConfig->EmulationMode == EMULATION_EXTERNAL && Config->TerminalConfig->EmulationFileName[0]))
  501.         {
  502.             StatusDisplayLeft    = WindowLeft;
  503.             StatusDisplayTop    = Window->Height - (Window->BorderBottom + StatusDisplayHeight);
  504.             StatusDisplayWidth    = WindowWidth;
  505.  
  506.             WindowHeight -= StatusDisplayHeight;
  507.         }
  508.     }
  509.  
  510.     if(ChatMode && !(Config->TerminalConfig->EmulationMode == EMULATION_EXTERNAL && Config->TerminalConfig->EmulationFileName[0]))
  511.     {
  512.         if(CreateChatGadget())
  513.         {
  514.             UpdateChatGadget();
  515.  
  516.             WindowHeight -= (UserFontHeight + 2);
  517.  
  518.             ActivateChat(FALSE);
  519.         }
  520.     }
  521. }
  522.  
  523.     /* Current2DefaultPalette(struct Configuration *SomeConfig):
  524.      *
  525.      *    Copy the current colour palette into the
  526.      *    default tables.
  527.      */
  528.  
  529. VOID
  530. Current2DefaultPalette(struct Configuration *SomeConfig)
  531. {
  532.     ColourTable    *Table;
  533.     UWORD *Colour12;
  534.  
  535.     Table = NULL;
  536.  
  537.     if(!SomeConfig)
  538.         SomeConfig = Config;
  539.  
  540.     switch(SomeConfig->ScreenConfig->ColourMode)
  541.     {
  542.         case COLOUR_EIGHT:
  543.  
  544.             if(Kick30)
  545.             {
  546.                 if(!ANSIColourTable)
  547.                     ANSIColourTable = CreateColourTable(8,ANSIColours,NULL);
  548.  
  549.                 Table = ANSIColourTable;
  550.             }
  551.  
  552.             Colour12 = ANSIColours;
  553.  
  554.             break;
  555.  
  556.         case COLOUR_SIXTEEN:
  557.  
  558.             if(Kick30)
  559.             {
  560.                 if(!EGAColourTable)
  561.                     EGAColourTable = CreateColourTable(16,EGAColours,NULL);
  562.  
  563.                 Table = EGAColourTable;
  564.             }
  565.  
  566.             Colour12 = EGAColours;
  567.  
  568.             break;
  569.  
  570.         case COLOUR_AMIGA:
  571.  
  572.             if(Kick30)
  573.             {
  574.                 if(!DefaultColourTable)
  575.                     DefaultColourTable = CreateColourTable(16,DefaultColours,NULL);
  576.  
  577.                 Table = DefaultColourTable;
  578.             }
  579.  
  580.             Colour12 = DefaultColours;
  581.  
  582.             break;
  583.  
  584.         case COLOUR_MONO:
  585.  
  586.             if(Kick30)
  587.             {
  588.                 if(!MonoColourTable)
  589.                     MonoColourTable = CreateColourTable(2,AtomicColours,NULL);
  590.  
  591.                 Table = MonoColourTable;
  592.             }
  593.  
  594.             Colour12 = AtomicColours;
  595.             break;
  596.     }
  597.  
  598.     if(Table)
  599.     {
  600.         if(SomeConfig->ScreenConfig->UseColours96)
  601.             Colour96xColourTable(SomeConfig->ScreenConfig->Colours96,Table,Table->NumColours);
  602.         else
  603.         {
  604.             Colour12xColourTable(SomeConfig->ScreenConfig->Colours,Table,Table->NumColours);
  605.  
  606.             Colour12x96(SomeConfig->ScreenConfig->Colours,SomeConfig->ScreenConfig->Colours96,16);
  607.  
  608.             SomeConfig->ScreenConfig->UseColours96 = TRUE;
  609.         }
  610.     }
  611.  
  612.     CopyMem(SomeConfig->ScreenConfig->Colours,Colour12,16 * sizeof(UWORD));
  613. }
  614.  
  615.     /* Default2CurrentPalette(struct Configuration *SomeConfig):
  616.      *
  617.      *    Copy the default palette to the current palette.
  618.      */
  619.  
  620. VOID
  621. Default2CurrentPalette(struct Configuration *SomeConfig)
  622. {
  623.     ColourTable    *Table;
  624.     UWORD *Colour12;
  625.  
  626.     Table = NULL;
  627.  
  628.     if(!SomeConfig)
  629.         SomeConfig = Config;
  630.  
  631.     switch(SomeConfig->ScreenConfig->ColourMode)
  632.     {
  633.         case COLOUR_EIGHT:
  634.  
  635.             Table        = ANSIColourTable;
  636.             Colour12    = ANSIColours;
  637.  
  638.             break;
  639.  
  640.         case COLOUR_SIXTEEN:
  641.  
  642.             Table        = EGAColourTable;
  643.             Colour12    = EGAColours;
  644.  
  645.             break;
  646.  
  647.         case COLOUR_AMIGA:
  648.  
  649.             Table        = DefaultColourTable;
  650.             Colour12    = DefaultColours;
  651.  
  652.             break;
  653.  
  654.         case COLOUR_MONO:
  655.  
  656.             Table        = MonoColourTable;
  657.             Colour12    = AtomicColours;
  658.  
  659.             break;
  660.     }
  661.  
  662.     CopyMem(Colour12,SomeConfig->ScreenConfig->Colours,16 * sizeof(UWORD));
  663.  
  664.     if(Table)
  665.     {
  666.         ColourTablex96(Table,SomeConfig->ScreenConfig->Colours96);
  667.  
  668.         SomeConfig->ScreenConfig->UseColours96 = TRUE;
  669.     }
  670.     else
  671.         SomeConfig->ScreenConfig->UseColours96 = FALSE;
  672. }
  673.  
  674.     /* PaletteSetup():
  675.      *
  676.      *    Set up colour palettes.
  677.      */
  678.  
  679. VOID
  680. PaletteSetup(struct Configuration *SomeConfig)
  681. {
  682.     LONG i;
  683.  
  684.     if(!SomeConfig)
  685.         SomeConfig = Config;
  686.  
  687.     if(SomeConfig->ScreenConfig->UseColours96)
  688.     {
  689.         Colour96x12(SomeConfig->ScreenConfig->Colours96,NormalColours,16);
  690.         Colour96x12(SomeConfig->ScreenConfig->Colours96,SomeConfig->ScreenConfig->Colours,16);
  691.     }
  692.     else
  693.     {
  694.         CopyMem(SomeConfig->ScreenConfig->Colours,NormalColours,16 * sizeof(UWORD));
  695.  
  696.         Colour12x96(NormalColours,SomeConfig->ScreenConfig->Colours96,16);
  697.  
  698.         SomeConfig->ScreenConfig->UseColours96 = TRUE;
  699.     }
  700.  
  701.     CopyMem(NormalColours,&NormalColours[16],16 * sizeof(UWORD));
  702.     CopyMem(NormalColours,BlinkColours,32 * sizeof(UWORD));
  703.  
  704.     switch(SomeConfig->ScreenConfig->ColourMode)
  705.     {
  706.         case COLOUR_EIGHT:
  707.  
  708.             if(SomeConfig->ScreenConfig->Blinking)
  709.             {
  710.                 for(i = 0 ; i < 8 ; i++)
  711.                     BlinkColours[8 + i] = BlinkColours[0];
  712.  
  713.                 PaletteSize = 16;
  714.             }
  715.             else
  716.                 PaletteSize = 8;
  717.  
  718.             break;
  719.  
  720.         case COLOUR_SIXTEEN:
  721.  
  722.             if(GetBitMapDepth(Window->WScreen->RastPort.BitMap) >= 5 && SomeConfig->ScreenConfig->Blinking)
  723.             {
  724.                 for(i = 0 ; i < 16 ; i++)
  725.                     BlinkColours[16 + i] = BlinkColours[0];
  726.  
  727.                 PaletteSize = 32;
  728.             }
  729.             else
  730.                 PaletteSize = 16;
  731.  
  732.             break;
  733.  
  734.         case COLOUR_AMIGA:
  735.  
  736.             BlinkColours[3] = BlinkColours[0];
  737.  
  738.             PaletteSize = 4;
  739.  
  740.             break;
  741.  
  742.         case COLOUR_MONO:
  743.  
  744.             PaletteSize = 2;
  745.             break;
  746.     }
  747.  
  748.     if(Kick30)
  749.     {
  750.         if(NormalColourTable)
  751.             DeleteColourTable(NormalColourTable);
  752.  
  753.         if(BlinkColourTable)
  754.             DeleteColourTable(BlinkColourTable);
  755.  
  756.         if(NormalColourTable = CreateColourTable(PaletteSize,NULL,SomeConfig->ScreenConfig->Colours96))
  757.         {
  758.             if(PaletteSize == 2 || !SomeConfig->ScreenConfig->Blinking)
  759.                 BlinkColourTable = NULL;
  760.             else
  761.             {
  762.                 if(BlinkColourTable = CreateColourTable(PaletteSize,NULL,NULL))
  763.                 {
  764.                     switch(SomeConfig->ScreenConfig->ColourMode)
  765.                     {
  766.                         case COLOUR_EIGHT:
  767.  
  768.                             for(i = 0 ; i < 8 ; i++)
  769.                             {
  770.                                 CopyColourEntry(NormalColourTable,NormalColourTable,i,8 + i);
  771.                                 CopyColourEntry(NormalColourTable,BlinkColourTable,i,i);
  772.                                 CopyColourEntry(NormalColourTable,BlinkColourTable,0,8 + i);
  773.                             }
  774.  
  775.                             break;
  776.  
  777.                         case COLOUR_SIXTEEN:
  778.  
  779.                             if(GetBitMapDepth(Window->WScreen->RastPort.BitMap) >= 5)
  780.                             {
  781.                                 for(i = 0 ; i < 16 ; i++)
  782.                                 {
  783.                                     CopyColourEntry(NormalColourTable,NormalColourTable,i,16 + i);
  784.                                     CopyColourEntry(NormalColourTable,BlinkColourTable,i,i);
  785.                                     CopyColourEntry(NormalColourTable,BlinkColourTable,0,16 + i);
  786.                                 }
  787.                             }
  788.                             else
  789.                             {
  790.                                 DeleteColourTable(BlinkColourTable);
  791.  
  792.                                 BlinkColourTable = NULL;
  793.                             }
  794.  
  795.                             break;
  796.  
  797.                         case COLOUR_AMIGA:
  798.  
  799.                             for(i = 0 ; i < 4 ; i++)
  800.                                 CopyColourEntry(NormalColourTable,BlinkColourTable,i,i);
  801.  
  802.                             CopyColourEntry(BlinkColourTable,BlinkColourTable,0,3);
  803.  
  804.                             break;
  805.                     }
  806.                 }
  807.                 else
  808.                 {
  809.                     DeleteColourTable(NormalColourTable);
  810.  
  811.                     NormalColourTable = NULL;
  812.                 }
  813.             }
  814.         }
  815.     }
  816. }
  817.  
  818.     /* ScreenSizeStuff():
  819.      *
  820.      *    Set up the terminal screen size.
  821.      */
  822.  
  823. VOID
  824. ScreenSizeStuff()
  825. {
  826.         /* Is this really the built-in emulation? */
  827.  
  828.     if(Config->TerminalConfig->EmulationMode != EMULATION_EXTERNAL)
  829.     {
  830.         LONG MaxColumns,MaxLines,Columns,Lines;
  831.  
  832.         MaxColumns    = WindowWidth / TextFontWidth;
  833.         MaxLines    = WindowHeight / TextFontHeight;
  834.  
  835.             /* Drop the text area marker. */
  836.  
  837.         if(Marking)
  838.             WindowMarkerStop();
  839.  
  840.             /* Turn off the cursor. */
  841.  
  842.         ClearCursor();
  843.  
  844.             /* Set up the new screen width. */
  845.  
  846.         if(Config->TerminalConfig->NumColumns < 20)
  847.             Columns = MaxColumns;
  848.         else
  849.             Columns = Config->TerminalConfig->NumColumns;
  850.  
  851.             /* Set up the new screen height. */
  852.  
  853.         if(Config->TerminalConfig->NumLines < 20)
  854.             Lines = MaxLines;
  855.         else
  856.             Lines = Config->TerminalConfig->NumLines;
  857.  
  858.             /* More columns than we will be able to display? */
  859.  
  860.         if(Columns > MaxColumns)
  861.             Columns = MaxColumns;
  862.  
  863.             /* More lines than we will be able to display? */
  864.  
  865.         if(Lines > MaxLines)
  866.             Lines = MaxLines;
  867.  
  868.             /* Set up the central data. */
  869.  
  870.         LastColumn    = Columns - 1;
  871.         LastLine    = Lines - 1;
  872.         LastPixel    = MUL_X(Columns) - 1;
  873.  
  874.             /* Are we to clear the margin? */
  875.  
  876.         if(Columns < MaxColumns || Lines < MaxLines)
  877.         {
  878.                 /* Save the rendering attributes. */
  879.  
  880.             BackupRender();
  881.  
  882.                 /* Set the defaults. */
  883.  
  884.             SetAPen(RPort,BgPen = MappedPens[0][PenTable[0]]);
  885.  
  886.             SetMask(RPort,DepthMask);
  887.  
  888.                 /* Clear remaining columns. */
  889.  
  890.             if(Columns < MaxColumns)
  891.                 ScrollLineRectFill(RPort,MUL_X(LastColumn + 1),0,WindowWidth - 1,WindowHeight - 1);
  892.  
  893.                 /* Clear remaining lines. */
  894.  
  895.             if(Lines < MaxLines)
  896.                 ScrollLineRectFill(RPort,0,MUL_Y(LastLine + 1),WindowWidth - 1,WindowHeight - 1);
  897.  
  898.                 /* Restore rendering attributes. */
  899.  
  900.             BackupRender();
  901.         }
  902.  
  903.             /* Truncate illegal cursor position. */
  904.  
  905.         if(CursorY > LastLine)
  906.             CursorY = LastLine;
  907.  
  908.         ConFontScaleUpdate();
  909.  
  910.             /* Truncate illegal cursor position. */
  911.  
  912.         if(CursorX > LastPrintableColumn)
  913.             CursorX = LastPrintableColumn;
  914.  
  915.             /* Fix scroll region button. */
  916.  
  917.         if(!RegionSet)
  918.             Bottom = LastLine;
  919.  
  920.             /* Turn the cursor back on. */
  921.  
  922.         DrawCursor();
  923.     }
  924.  
  925.     FixScreenSize = FALSE;
  926. }
  927.  
  928.     /* PubScreenStuff():
  929.      *
  930.      *    This part handles the public screen setup stuff.
  931.      */
  932.  
  933. VOID
  934. PubScreenStuff()
  935. {
  936.     if(Screen)
  937.     {
  938.             /* Are we to make our screen public? */
  939.  
  940.         if(Config->ScreenConfig->MakeScreenPublic)
  941.             PubScreenStatus(Screen,NULL);
  942.         else
  943.             PubScreenStatus(Screen,PSNF_PRIVATE);
  944.     }
  945. }
  946.  
  947.     /* ConfigSetup():
  948.      *
  949.      *    Compare the current configuration with the
  950.      *    last backup and reset the serial device, terminal,
  951.      *    etc. if necessary.
  952.      */
  953.  
  954. VOID
  955. ConfigSetup()
  956. {
  957.     BOOL RasterWasEnabled,WindowSizeUpdate,WantCustomScreen;
  958.  
  959.     RasterWasEnabled = RasterEnabled;
  960.     WindowSizeUpdate = FALSE;
  961.  
  962.         /* See if we want a custom screen */
  963.  
  964.     if(Config->ScreenConfig->UseWorkbench || Config->ScreenConfig->ShareScreen)
  965.         WantCustomScreen = FALSE;
  966.     else
  967.         WantCustomScreen = TRUE;
  968.  
  969.         /* Hide or show the upload queue icon. */
  970.  
  971.     ToggleUploadQueueIcon(Config->TransferConfig->HideUploadIcon);
  972.  
  973.         /* Take care of the end-of-line translation. */
  974.  
  975.     Update_CR_LF_Translation();
  976.  
  977.         /* First we will take a look at the configuration
  978.          * and try to find those parts which have changed
  979.          * and require the main screen display to be
  980.          * reopened.
  981.          */
  982.  
  983.     if(PrivateConfig->ScreenConfig->FontHeight != Config->ScreenConfig->FontHeight)
  984.         ResetDisplay = TRUE;
  985.  
  986.     if((PrivateConfig->TerminalConfig->EmulationMode == EMULATION_EXTERNAL && Config->TerminalConfig->EmulationMode != EMULATION_EXTERNAL) || (PrivateConfig->TerminalConfig->EmulationMode != EMULATION_EXTERNAL && Config->TerminalConfig->EmulationMode == EMULATION_EXTERNAL))
  987.         ResetDisplay = TRUE;
  988.  
  989.     if(PrivateConfig->ScreenConfig->SplitStatus != Config->ScreenConfig->SplitStatus)
  990.         ResetDisplay = TRUE;
  991.  
  992.     if(PrivateConfig->ScreenConfig->ShareScreen != Config->ScreenConfig->ShareScreen)
  993.         ResetDisplay = TRUE;
  994.  
  995.     if(PrivateConfig->ScreenConfig->Depth != Config->ScreenConfig->Depth || PrivateConfig->ScreenConfig->OverscanType != Config->ScreenConfig->OverscanType)
  996.         ResetDisplay = TRUE;
  997.  
  998.     if(PrivateConfig->ScreenConfig->DisplayWidth != Config->ScreenConfig->DisplayWidth || PrivateConfig->ScreenConfig->DisplayHeight != Config->ScreenConfig->DisplayHeight)
  999.         ResetDisplay = TRUE;
  1000.  
  1001.     if(Stricmp(PrivateConfig->ScreenConfig->FontName,Config->ScreenConfig->FontName))
  1002.         ResetDisplay = TRUE;
  1003.  
  1004.     if(PrivateConfig->TerminalConfig->FontMode != Config->TerminalConfig->FontMode)
  1005.         ResetDisplay = TRUE;
  1006.  
  1007.     if(PrivateConfig->TerminalConfig->AutoSize != Config->TerminalConfig->AutoSize && !WantCustomScreen)
  1008.     {
  1009.         if(Config->TerminalConfig->AutoSize)
  1010.             FixScreenSize = TRUE;
  1011.         else
  1012.             WindowSizeUpdate = TRUE;
  1013.     }
  1014.  
  1015.     if(PrivateConfig->TerminalConfig->TextFontHeight != Config->TerminalConfig->TextFontHeight)
  1016.         ResetDisplay = TRUE;
  1017.  
  1018.     if(Stricmp(PrivateConfig->TerminalConfig->TextFontName,Config->TerminalConfig->TextFontName))
  1019.         ResetDisplay = TRUE;
  1020.  
  1021.     if(PrivateConfig->ScreenConfig->DisplayMode != Config->ScreenConfig->DisplayMode || PrivateConfig->ScreenConfig->ColourMode != Config->ScreenConfig->ColourMode)
  1022.         ResetDisplay = TRUE;
  1023.  
  1024.     if(PrivateConfig->TerminalConfig->IBMFontHeight != Config->TerminalConfig->IBMFontHeight)
  1025.         ResetDisplay = TRUE;
  1026.  
  1027.     if(Stricmp(PrivateConfig->TerminalConfig->IBMFontName,Config->TerminalConfig->IBMFontName))
  1028.         ResetDisplay = TRUE;
  1029.  
  1030.     if((PrivateConfig->ScreenConfig->ColourMode == Config->ScreenConfig->ColourMode) && (PrivateConfig->ScreenConfig->ColourMode == COLOUR_EIGHT || PrivateConfig->ScreenConfig->ColourMode == COLOUR_SIXTEEN))
  1031.     {
  1032.         if(PrivateConfig->ScreenConfig->Blinking != Config->ScreenConfig->Blinking)
  1033.             ResetDisplay = TRUE;
  1034.     }
  1035.  
  1036.     if(Kick30)
  1037.     {
  1038.         if(Config->ScreenConfig->UsePens != PrivateConfig->ScreenConfig->UsePens || memcmp(Config->ScreenConfig->PenArray,PrivateConfig->ScreenConfig->PenArray,sizeof(UWORD) * 12))
  1039.             ResetDisplay = TRUE;
  1040.     }
  1041.  
  1042.     if(Config->ScreenConfig->Depth != PrivateConfig->ScreenConfig->Depth)
  1043.         ResetDisplay = TRUE;
  1044.  
  1045.     if(PrivateConfig->ScreenConfig->StatusLine != Config->ScreenConfig->StatusLine)
  1046.         ResetDisplay = TRUE;
  1047.  
  1048.     if(PrivateConfig->ScreenConfig->TitleBar != Config->ScreenConfig->TitleBar)
  1049.         ResetDisplay = TRUE;
  1050.  
  1051.     if(PrivateConfig->ScreenConfig->UseWorkbench != Config->ScreenConfig->UseWorkbench)
  1052.         ResetDisplay = TRUE;
  1053.  
  1054.     if(strcmp(PrivateConfig->ScreenConfig->PubScreenName,Config->ScreenConfig->PubScreenName) && Config->ScreenConfig->UseWorkbench)
  1055.         ResetDisplay = TRUE;
  1056.  
  1057.     if(PrivateConfig->EmulationConfig->UseStandardPens != Config->EmulationConfig->UseStandardPens)
  1058.         ResetDisplay = TRUE;
  1059.  
  1060.     if(!Config->EmulationConfig->UseStandardPens && (memcmp(Config->EmulationConfig->Pens,PrivateConfig->EmulationConfig->Pens,sizeof(Config->EmulationConfig->Pens)) || memcmp(Config->EmulationConfig->Attributes,PrivateConfig->EmulationConfig->Attributes,sizeof(Config->EmulationConfig->Attributes))))
  1061.         ResetDisplay = TRUE;
  1062.  
  1063.     if(PrivateConfig->ScreenConfig->UseSimpleRefresh != Config->ScreenConfig->UseSimpleRefresh)
  1064.         ResetDisplay = TRUE;
  1065.  
  1066.         /* Now for the `harmless' actions which do not
  1067.          * require to change the screen or other
  1068.          * rendering data.
  1069.          */
  1070.  
  1071.     if(!ResetDisplay)
  1072.     {
  1073.         if(PrivateConfig->TerminalConfig->NumColumns != Config->TerminalConfig->NumColumns || PrivateConfig->TerminalConfig->NumLines != Config->TerminalConfig->NumLines)
  1074.         {
  1075.             if(!WantCustomScreen)
  1076.                 WindowSizeUpdate = TRUE;
  1077.             else
  1078.                 ResetDisplay = TRUE;
  1079.         }
  1080.     }
  1081.  
  1082.     if(!ResetDisplay && WindowSizeUpdate)
  1083.     {
  1084.         LONG    Width,
  1085.             Height;
  1086.  
  1087.         if(Config->TerminalConfig->NumColumns < 20)
  1088.             Width = ScreenWidth;
  1089.         else
  1090.             Width = Window->BorderLeft + TextFontWidth * Config->TerminalConfig->NumColumns + Window->BorderRight;
  1091.  
  1092.         if(Config->TerminalConfig->NumLines < 20)
  1093.             Height = ScreenHeight;
  1094.         else
  1095.             Height = Window->BorderTop + TextFontHeight * Config->TerminalConfig->NumLines + Window->BorderBottom;
  1096.  
  1097.             /* Add the status line size if necessary */
  1098.  
  1099.         if(!StatusWindow && Config->ScreenConfig->StatusLine != STATUSLINE_DISABLED && !(Config->TerminalConfig->EmulationMode == EMULATION_EXTERNAL && Config->TerminalConfig->EmulationFileName[0]))
  1100.             Height += StatusDisplayHeight;
  1101.  
  1102.         ChangeWindowBox(Window,Window->LeftEdge,Window->TopEdge,Width,Height);
  1103.  
  1104.         FixScreenSize = TRUE;
  1105.     }
  1106.  
  1107.     if(PrivateConfig->ScreenConfig->MakeScreenPublic != Config->ScreenConfig->MakeScreenPublic)
  1108.         PubScreenStuff();
  1109.  
  1110.     if(PrivateConfig->ScreenConfig->ColourMode == Config->ScreenConfig->ColourMode && (memcmp(PrivateConfig->ScreenConfig->Colours,Config->ScreenConfig->Colours,sizeof(UWORD) * 16) || memcmp(PrivateConfig->ScreenConfig->Colours96,Config->ScreenConfig->Colours96,sizeof(ColourEntry) * 16)))
  1111.         Current2DefaultPalette(Config);
  1112.  
  1113.         /* Are we to load a new transfer library? */
  1114.  
  1115.     if(Config->TransferConfig->DefaultType == XFER_XPR)
  1116.     {
  1117.         if(Config->TransferConfig->DefaultLibrary[0])
  1118.         {
  1119.             if(strcmp(PrivateConfig->TransferConfig->DefaultLibrary,Config->TransferConfig->DefaultLibrary))
  1120.             {
  1121.                 strcpy(LastXprLibrary,Config->TransferConfig->DefaultLibrary);
  1122.  
  1123.                 ProtocolSetup(FALSE);
  1124.             }
  1125.         }
  1126.         else
  1127.             CloseXPR();
  1128.     }
  1129.     else
  1130.     {
  1131.         CloseXPR();
  1132.  
  1133.         ProtocolSetup(FALSE);
  1134.     }
  1135.  
  1136.         /* No custom keymap this time? */
  1137.  
  1138.     if(!Config->TerminalConfig->KeyMapFileName[0])
  1139.     {
  1140.         KeyMap = NULL;
  1141.  
  1142.         if(KeySegment)
  1143.         {
  1144.             UnLoadSeg(KeySegment);
  1145.  
  1146.             KeySegment = NULL;
  1147.         }
  1148.     }
  1149.     else
  1150.     {
  1151.             /* Check whether the keymap name has changed. */
  1152.  
  1153.         if(strcmp(PrivateConfig->TerminalConfig->KeyMapFileName,Config->TerminalConfig->KeyMapFileName))
  1154.             KeyMap = LoadKeyMap(Config->TerminalConfig->KeyMapFileName);
  1155.     }
  1156.  
  1157.         /* Are we to load the keyboard macro settings? */
  1158.  
  1159.     if(Config->MacroFileName[0] && Stricmp(PrivateConfig->MacroFileName,Config->MacroFileName))
  1160.     {
  1161.         if(!LoadMacros(Config->MacroFileName,MacroKeys))
  1162.             ResetMacroKeys(MacroKeys);
  1163.         else
  1164.             strcpy(LastMacros,Config->MacroFileName);
  1165.     }
  1166.  
  1167.         /* Are we to load the cursor key settings? */
  1168.  
  1169.     if(Config->CursorFileName[0] && Stricmp(PrivateConfig->CursorFileName,Config->CursorFileName))
  1170.     {
  1171.         if(!ReadIFFData(Config->CursorFileName,CursorKeys,sizeof(struct CursorKeys),ID_KEYS))
  1172.             ResetCursorKeys(CursorKeys);
  1173.         else
  1174.             strcpy(LastCursorKeys,Config->CursorFileName);
  1175.     }
  1176.  
  1177.         /* Are we to load the translation tables? */
  1178.  
  1179.     if(Config->TranslationFileName[0] && Stricmp(PrivateConfig->TranslationFileName,Config->TranslationFileName))
  1180.     {
  1181.         if(SendTable)
  1182.         {
  1183.             FreeTranslationTable(SendTable);
  1184.  
  1185.             SendTable = NULL;
  1186.         }
  1187.  
  1188.         if(ReceiveTable)
  1189.         {
  1190.             FreeTranslationTable(ReceiveTable);
  1191.  
  1192.             ReceiveTable = NULL;
  1193.         }
  1194.  
  1195.         if(SendTable = AllocTranslationTable())
  1196.         {
  1197.             if(ReceiveTable = AllocTranslationTable())
  1198.             {
  1199.                 if(LoadTranslationTables(Config->TranslationFileName,SendTable,ReceiveTable))
  1200.                 {
  1201.                     strcpy(LastTranslation,Config->TranslationFileName);
  1202.  
  1203.                     if(IsStandardTable(SendTable) && IsStandardTable(ReceiveTable))
  1204.                     {
  1205.                         FreeTranslationTable(SendTable);
  1206.  
  1207.                         SendTable = NULL;
  1208.  
  1209.                         FreeTranslationTable(ReceiveTable);
  1210.  
  1211.                         ReceiveTable = NULL;
  1212.                     }
  1213.                 }
  1214.                 else
  1215.                 {
  1216.                     FreeTranslationTable(SendTable);
  1217.  
  1218.                     SendTable = NULL;
  1219.  
  1220.                     FreeTranslationTable(ReceiveTable);
  1221.  
  1222.                     ReceiveTable = NULL;
  1223.                 }
  1224.             }
  1225.             else
  1226.             {
  1227.                 FreeTranslationTable(SendTable);
  1228.  
  1229.                 SendTable = NULL;
  1230.             }
  1231.         }
  1232.     }
  1233.  
  1234.         /* Update the text sending functions. */
  1235.  
  1236.     SendSetup();
  1237.  
  1238.         /* Are we to load the fast macro settings? */
  1239.  
  1240.     if(Config->FastMacroFileName[0] && Stricmp(PrivateConfig->FastMacroFileName,Config->FastMacroFileName))
  1241.     {
  1242.         if(FastWindow)
  1243.             LT_LockWindow(FastWindow);
  1244.  
  1245.         FreeList(&FastMacroList);
  1246.  
  1247.         if(LoadFastMacros(Config->FastMacroFileName,&FastMacroList))
  1248.         {
  1249.             strcpy(LastFastMacros,Config->FastMacroFileName);
  1250.             FastMacroCount = GetListSize(&FastMacroList);
  1251.         }
  1252.  
  1253.         if(FastWindow)
  1254.         {
  1255.             RefreshFastWindow();
  1256.  
  1257.             LT_UnlockWindow(FastWindow);
  1258.         }
  1259.     }
  1260.  
  1261.         /* Serial configuration needs updating? */
  1262.  
  1263.     ReconfigureSerial(Window,NULL);
  1264.  
  1265.         /* Are we to open the fast macro panel? */
  1266.  
  1267.     if(Config->MiscConfig->OpenFastMacroPanel)
  1268.         HadFastMacros = TRUE;
  1269.  
  1270.         /* Are we to freeze the text buffer? */
  1271.  
  1272.     if(!Config->CaptureConfig->BufferEnabled)
  1273.         BufferFrozen = TRUE;
  1274.  
  1275.         /* Now for the actions which require that the
  1276.          * screen stays open.
  1277.          */
  1278.  
  1279.     if(!ResetDisplay)
  1280.     {
  1281.         if(Config->TerminalConfig->EmulationMode == EMULATION_EXTERNAL)
  1282.         {
  1283.             if(PrivateConfig->TerminalConfig->EmulationMode != EMULATION_EXTERNAL || (Config->TerminalConfig->EmulationFileName[0] && strcmp(PrivateConfig->TerminalConfig->EmulationFileName,Config->TerminalConfig->EmulationFileName)))
  1284.             {
  1285.                 if(!OpenEmulator(Config->TerminalConfig->EmulationFileName))
  1286.                 {
  1287.                     Config->TerminalConfig->EmulationMode = EMULATION_ANSIVT100;
  1288.  
  1289.                     ResetDisplay = TRUE;
  1290.  
  1291.                     RasterEnabled = TRUE;
  1292.  
  1293.                     ShowRequest(Window,LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_EMULATION_LIBRARY_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),Config->TerminalConfig->EmulationFileName);
  1294.                 }
  1295.                 else
  1296.                     RasterEnabled = FALSE;
  1297.             }
  1298.         }
  1299.         else
  1300.         {
  1301.             if(XEmulatorBase && PrivateConfig->TerminalConfig->EmulationMode == EMULATION_EXTERNAL)
  1302.             {
  1303.                 XEmulatorClearConsole(XEM_IO);
  1304.  
  1305.                 CloseEmulator(FALSE);
  1306.             }
  1307.             else
  1308.                 RasterEnabled = TRUE;
  1309.         }
  1310.  
  1311.         if(RasterEnabled != RasterWasEnabled)
  1312.             RasterEraseScreen(2);
  1313.  
  1314.         if(!Config->ScreenConfig->UseWorkbench && !SharedScreen)
  1315.         {
  1316.             PaletteSetup(Config);
  1317.  
  1318.             LoadColourTable(VPort,NormalColourTable,NormalColours,PaletteSize);
  1319.         }
  1320.  
  1321.         if(Config->MiscConfig->OpenFastMacroPanel && !FastWindow)
  1322.             OpenFastWindow();
  1323.  
  1324.         PubScreenStuff();
  1325.  
  1326.         if(Menu)
  1327.         {
  1328.             CheckItem(MEN_FREEZE_BUFFER,BufferFrozen);
  1329.  
  1330.             if(Config->TerminalConfig->EmulationMode == EMULATION_EXTERNAL && Config->TerminalConfig->EmulationFileName[0])
  1331.                 OffItem(MEN_CHAT_LINE);
  1332.             else
  1333.                 CheckItem(MEN_CHAT_LINE,ChatMode);
  1334.  
  1335.             SetTransferMenu(TRUE);
  1336.  
  1337.             SetRasterMenu(RasterEnabled);
  1338.  
  1339.             if(MatrixWindow)
  1340.                 CheckItem(MEN_MATRIX_WINDOW,TRUE);
  1341.  
  1342.             if(InfoWindow)
  1343.                 CheckItem(MEN_STATUS_WINDOW,TRUE);
  1344.  
  1345.             if(PacketWindow)
  1346.                 CheckItem(MEN_PACKET_WINDOW,TRUE);
  1347.  
  1348.             if(ChatMode)
  1349.                 CheckItem(MEN_CHAT_LINE,TRUE);
  1350.  
  1351.             if(FastWindow)
  1352.                 CheckItem(MEN_FAST_MACROS_WINDOW,TRUE);
  1353.  
  1354.                 /* Disable the dialing functions if online. */
  1355.  
  1356.             if(Online)
  1357.                 SetDialMenu(FALSE);
  1358.             else
  1359.                 SetDialMenu(TRUE);
  1360.         }
  1361.  
  1362.         Blocking = FALSE;
  1363.     }
  1364.     else
  1365.     {
  1366.             /* Are we no longer to use the external emulator? */
  1367.  
  1368.         if(Config->TerminalConfig->EmulationMode != EMULATION_EXTERNAL)
  1369.         {
  1370.             if(XEmulatorBase && PrivateConfig->TerminalConfig->EmulationMode == EMULATION_EXTERNAL)
  1371.             {
  1372.                 XEmulatorClearConsole(XEM_IO);
  1373.  
  1374.                 CloseEmulator(FALSE);
  1375.             }
  1376.         }
  1377.  
  1378.         RasterEnabled = TRUE;
  1379.         FixScreenSize = FALSE;
  1380.     }
  1381.  
  1382.         /* Change the task priority. */
  1383.  
  1384.     SetTaskPri((struct Task *)ThisProcess,(LONG)Config->MiscConfig->Priority);
  1385.  
  1386.     ConOutputUpdate();
  1387.  
  1388.     ConFontScaleUpdate();
  1389.  
  1390.     ConProcessUpdate();
  1391.  
  1392.         /* Reset the scanner. */
  1393.  
  1394.     ResetDataFlowFilter();
  1395.  
  1396.     if(ResetDisplay)
  1397.         ActivateJob(MainJobQueue,ResetDisplayJob);
  1398. }
  1399.  
  1400.     /* DisplayReset():
  1401.      *
  1402.      *    Reset the entire display if necessary.
  1403.      */
  1404.  
  1405. BOOL
  1406. DisplayReset()
  1407. {
  1408.     BOOL Success = TRUE;
  1409.     STRPTR Result;
  1410.  
  1411.         /* Delete the display (if possible).
  1412.          * This will go wrong if there
  1413.          * are any visitor windows on our
  1414.          * screen.
  1415.          */
  1416.  
  1417.     if(DeleteDisplay())
  1418.     {
  1419.         if(Result = CreateDisplay(FALSE,FALSE))
  1420.         {
  1421.             DeleteDisplay();
  1422.  
  1423.             ShowRequest(NULL,LocaleString(MSG_GLOBAL_TERM_HAS_A_PROBLEM_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),Result);
  1424.  
  1425.             Success = FALSE;
  1426.         }
  1427.         else
  1428.         {
  1429.             BumpWindow(Window);
  1430.  
  1431.             PubScreenStuff();
  1432.         }
  1433.     }
  1434.     else
  1435.     {
  1436.         SaveConfig(PrivateConfig,Config);
  1437.  
  1438.         BlockWindows();
  1439.         ShowRequest(Window,LocaleString(MSG_GLOBAL_TERM_HAS_A_PROBLEM_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),LocaleString(MSG_TERMMAIN_CANNOT_CLOSE_SCREEN_YET_TXT));
  1440.         ReleaseWindows();
  1441.     }
  1442.  
  1443.     ResetDisplay = FALSE;
  1444.     SuspendJob(MainJobQueue,ResetDisplayJob);
  1445.  
  1446.         /* Prepare for the worst case... */
  1447.  
  1448.     if(!Success)
  1449.         MainTerminated = TRUE;
  1450.  
  1451.     return(Success);
  1452. }
  1453.  
  1454.     /* DeleteDisplay():
  1455.      *
  1456.      *    Free all resources associated with the terminal
  1457.      *    display (tasks, interrupts, screen, window, etc.).
  1458.      */
  1459.  
  1460. BOOL
  1461. DeleteDisplay()
  1462. {
  1463.     struct Screen *WhichScreen;
  1464.  
  1465.     if(SharedScreen)
  1466.         WhichScreen = SharedScreen;
  1467.     else
  1468.         WhichScreen = Screen;
  1469.  
  1470.     if(WhichScreen)
  1471.     {
  1472.         if(!(PubScreenStatus(WhichScreen,PSNF_PRIVATE) & PSNF_PRIVATE))
  1473.             return(FALSE);
  1474.     }
  1475.  
  1476.     StopTerminalTest();
  1477.  
  1478.     GuideCleanup();
  1479.  
  1480.     CloseQueueWindow();
  1481.  
  1482.     ShakeHands((struct Task *)StatusProcess,SIG_KILL);
  1483.  
  1484.     WindowMarkerStop();
  1485.  
  1486.     CloseInfoWindow();
  1487.  
  1488.     DeleteReview();
  1489.  
  1490.     CloseEmulator(FALSE);
  1491.  
  1492.     DeleteRaster();
  1493.  
  1494.     DeleteScale();
  1495.  
  1496.     FreeVecPooled(TabStops);
  1497.     TabStops = NULL;
  1498.  
  1499.     FreeVecPooled(ScrollLines);
  1500.     ScrollLines = NULL;
  1501.  
  1502.     if(Screen)
  1503.         ScreenToBack(Screen);
  1504.  
  1505.     if(FastWindow)
  1506.     {
  1507.         HadFastMacros = TRUE;
  1508.  
  1509.         CloseFastWindow();
  1510.     }
  1511.     else
  1512.         HadFastMacros = FALSE;
  1513.  
  1514.     if(MatrixWindow)
  1515.         CloseMatrixWindow();
  1516.  
  1517.     if(StatusWindow)
  1518.     {
  1519.         ClearMenuStrip(StatusWindow);
  1520.         CloseWindowSafely(StatusWindow);
  1521.  
  1522.         StatusWindow = NULL;
  1523.     }
  1524.  
  1525.     if(DrawInfo)
  1526.     {
  1527.             /* Release the rendering pens. */
  1528.  
  1529.         FreeScreenDrawInfo(Window->WScreen,DrawInfo);
  1530.  
  1531.         DrawInfo = NULL;
  1532.     }
  1533.  
  1534.     HideChatGadget();
  1535.  
  1536.     DeletePacketWindow(FALSE);
  1537.  
  1538.     if(Window)
  1539.     {
  1540.         if(!Screen)
  1541.             PutWindowInfo(WINDOW_MAIN,Window->LeftEdge,Window->TopEdge,Window->Width,Window->Height);
  1542.  
  1543.         if(AllocatedPens && Kick30)
  1544.         {
  1545.             LONG i;
  1546.  
  1547.                 /* Erase the window contents. We will
  1548.                  * want to release any pens we have
  1549.                  * allocated and want to avoid nasty
  1550.                  * flashing and flickering.
  1551.                  */
  1552.  
  1553.             SetAPen(RPort,0);
  1554.  
  1555.             FillBox(RPort,WindowLeft,WindowTop,WindowWidth,WindowHeight);
  1556.  
  1557.                 /* Release any pens we have allocated. */
  1558.  
  1559.             for(i = 0 ; i < 32 ; i++)
  1560.             {
  1561.                 if(MappedPens[1][i])
  1562.                 {
  1563.                     ReleasePen(VPort->ColorMap,MappedPens[0][i]);
  1564.  
  1565.                     MappedPens[0][i] = i;
  1566.                     MappedPens[1][i] = FALSE;
  1567.                 }
  1568.             }
  1569.  
  1570.             AllocatedPens = FALSE;
  1571.         }
  1572.  
  1573.         if(ClipRegion)
  1574.         {
  1575.             InstallClipRegion(Window->WLayer,OldRegion);
  1576.  
  1577.             DisposeRegion(ClipRegion);
  1578.  
  1579.             ClipRegion = NULL;
  1580.         }
  1581.  
  1582.         ClearMenuStrip(Window);
  1583.  
  1584.         RestoreWindowPtr(OldWindowPtr);
  1585.  
  1586.         PopWindow();
  1587.  
  1588.         if(TermPort)
  1589.             TermPort->TopWindow = NULL;
  1590.  
  1591.         LT_DeleteWindowLock(Window);
  1592.  
  1593.         CloseWindow(Window);
  1594.  
  1595.         Window = NULL;
  1596.     }
  1597.  
  1598.     if(Menu)
  1599.     {
  1600.         LT_DisposeMenu(Menu);
  1601.         Menu = NULL;
  1602.     }
  1603.  
  1604.     FreeVisualInfo(VisualInfo);
  1605.     VisualInfo = NULL;
  1606.  
  1607.         /* Clean up the menu glyphs. */
  1608.  
  1609.     DisposeObject(AmigaGlyph);
  1610.  
  1611.     AmigaGlyph = NULL;
  1612.  
  1613.     DisposeObject(CheckGlyph);
  1614.  
  1615.     CheckGlyph = NULL;
  1616.  
  1617.     if(Screen)
  1618.     {
  1619.         CloseScreen(Screen);
  1620.  
  1621.         Screen = NULL;
  1622.     }
  1623.  
  1624.     if(SharedScreen)
  1625.     {
  1626.         CloseScreen(SharedScreen);
  1627.  
  1628.         SharedScreen = NULL;
  1629.     }
  1630.  
  1631.     ReleaseDefaultPubScreen();
  1632.  
  1633.     if(GFX)
  1634.     {
  1635.         CloseFont(GFX);
  1636.  
  1637.         GFX = NULL;
  1638.     }
  1639.  
  1640.     if(TextFont)
  1641.     {
  1642.         CloseFont(TextFont);
  1643.  
  1644.         TextFont = NULL;
  1645.     }
  1646.  
  1647.     if(UserTextFont)
  1648.     {
  1649.         CloseFont(UserTextFont);
  1650.  
  1651.         UserTextFont = NULL;
  1652.     }
  1653.  
  1654.     return(TRUE);
  1655. }
  1656.  
  1657.     /* CreateDisplay(BOOL UsePresetSize):
  1658.      *
  1659.      *    Open the display and allocate associated data.
  1660.      */
  1661.  
  1662. STRPTR
  1663. CreateDisplay(BOOL UsePresetSize,BOOL Activate)
  1664. {
  1665.     LONG ErrorCode,Top,Height,Count,RealDepth,i;
  1666.     LONG StatusWidth,StatusHeight;
  1667.     struct Rectangle DisplayClip;
  1668.     UWORD PenArray[16];
  1669.     ULONG X_DPI,Y_DPI;
  1670.     ULONG TagArray[9];
  1671.     BOOL OpenFailed;
  1672.     BOOL RethinkPens;
  1673.  
  1674.     OpenFailed = FALSE;
  1675.     Count = 0;
  1676.  
  1677.     ResetDisplay = FALSE;
  1678.     SuspendJob(MainJobQueue,ResetDisplayJob);
  1679.  
  1680.     BlockNestCount = 0;
  1681.  
  1682.     WeAreBlocking = FALSE;
  1683.  
  1684.     SetQueueDiscard(SpecialQueue,FALSE);
  1685.  
  1686.     TagDPI[0].ti_Tag = TAG_DONE;
  1687.  
  1688.         /* Don't permit weird settings. */
  1689.  
  1690.     if(!Config->ScreenConfig->StatusLine || (!Config->ScreenConfig->ShareScreen && !Config->ScreenConfig->UseWorkbench))
  1691.         Config->ScreenConfig->SplitStatus = FALSE;
  1692.  
  1693.     if(Config->ScreenConfig->UseWorkbench || Config->ScreenConfig->ShareScreen)
  1694.     {
  1695.         STRPTR ScreenName = NULL;
  1696.  
  1697.         if(Config->ScreenConfig->PubScreenName[0])
  1698.         {
  1699.             struct Screen *SomeScreen;
  1700.  
  1701.             if(SomeScreen = LockPubScreen(Config->ScreenConfig->PubScreenName))
  1702.             {
  1703.                 UnlockPubScreen(NULL,SomeScreen);
  1704.  
  1705.                 ScreenName = Config->ScreenConfig->PubScreenName;
  1706.             }
  1707.         }
  1708.  
  1709.         if(!(DefaultPubScreen = LockPubScreen(ScreenName)))
  1710.             return(LocaleString(MSG_TERMINIT_FAILED_TO_GET_DEFAULT_PUBLIC_SCREEN_TXT));
  1711.         else
  1712.         {
  1713.             GetDPI(GetVPModeID(&DefaultPubScreen->ViewPort),&X_DPI,&Y_DPI);
  1714.  
  1715.             strcpy(UserFontName,DefaultPubScreen->Font->ta_Name);
  1716.  
  1717.             UserFont.tta_Name    = UserFontName;
  1718.             UserFont.tta_YSize    = DefaultPubScreen->Font->ta_YSize;
  1719.             UserFont.tta_Style    = DefaultPubScreen->Font->ta_Style;
  1720.             UserFont.tta_Flags    = DefaultPubScreen->Font->ta_Flags;
  1721.         }
  1722.     }
  1723.  
  1724.     if(!Config->ScreenConfig->UseWorkbench)
  1725.     {
  1726.         GetDPI(Config->ScreenConfig->DisplayMode,&X_DPI,&Y_DPI);
  1727.  
  1728.         strcpy(UserFontName,Config->ScreenConfig->FontName);
  1729.  
  1730.         UserFont.tta_Name    = UserFontName;
  1731.         UserFont.tta_YSize    = Config->ScreenConfig->FontHeight;
  1732.         UserFont.tta_Style    = FS_NORMAL | FSF_TAGGED;
  1733.         UserFont.tta_Flags    = FPF_DESIGNED;
  1734.         UserFont.tta_Tags    = TagDPI;
  1735.  
  1736.         TagDPI[0].ti_Tag     = TA_DeviceDPI;
  1737.         TagDPI[0].ti_Data     = (X_DPI << 16) | Y_DPI;
  1738.         TagDPI[1].ti_Tag     = TAG_DONE;
  1739.     }
  1740.  
  1741.     if(!(UserTextFont = SmartOpenDiskFont((struct TextAttr *)&UserFont)))
  1742.     {
  1743.         if(Config->ScreenConfig->UseWorkbench)
  1744.         {
  1745.             ReleaseDefaultPubScreen();
  1746.  
  1747.             return(LocaleString(MSG_TERMINIT_UNABLE_TO_OPEN_FONT_TXT));
  1748.         }
  1749.         else
  1750.         {
  1751.             strcpy(Config->ScreenConfig->FontName,    "topaz.font");
  1752.             strcpy(UserFontName,                    "topaz.font");
  1753.             Config->ScreenConfig->FontHeight = 8;
  1754.  
  1755.             UserFont.tta_YSize    = 8;
  1756.             UserFont.tta_Style    = FS_NORMAL;
  1757.             UserFont.tta_Flags    = FPF_DESIGNED | FPF_ROMFONT;
  1758.  
  1759.             if(!(UserTextFont = OpenFont((struct TextAttr *)&UserFont)))
  1760.             {
  1761.                 ReleaseDefaultPubScreen();
  1762.  
  1763.                 return(LocaleString(MSG_TERMINIT_UNABLE_TO_OPEN_FONT_TXT));
  1764.             }
  1765.         }
  1766.     }
  1767.  
  1768.         /* Open the default text rendering font. */
  1769.  
  1770.     do
  1771.     {
  1772.             /* If it's not the standard font, use the IBM PC style font. */
  1773.  
  1774.         if(Config->TerminalConfig->FontMode != FONT_STANDARD)
  1775.         {
  1776.             strcpy(TextFontName,Config->TerminalConfig->IBMFontName);
  1777.  
  1778.             TextAttr.tta_YSize = Config->TerminalConfig->IBMFontHeight;
  1779.         }
  1780.         else
  1781.         {
  1782.             strcpy(TextFontName,Config->TerminalConfig->TextFontName);
  1783.  
  1784.             TextAttr.tta_YSize = Config->TerminalConfig->TextFontHeight;
  1785.         }
  1786.  
  1787.         TextAttr.tta_Name    = TextFontName;
  1788.         TextAttr.tta_Style    = FS_NORMAL | FSF_TAGGED;
  1789.         TextAttr.tta_Flags    = FPF_DESIGNED;
  1790.         TextAttr.tta_Tags    = TagDPI;
  1791.  
  1792.             /* Does it open? */
  1793.  
  1794.         if(!(TextFont = SmartOpenDiskFont((struct TextAttr *)&TextAttr)))
  1795.         {
  1796.                 /* So it didn't open. Revert to the standard text font. */
  1797.  
  1798.             if(Config->TerminalConfig->FontMode != FONT_STANDARD)
  1799.                 Config->TerminalConfig->FontMode = FONT_STANDARD;
  1800.             else
  1801.             {
  1802.                     /* So this is the standard font. Is it the ROM default font? */
  1803.  
  1804.                 if(!Stricmp(Config->TerminalConfig->TextFontName,"topaz.font") && Config->TerminalConfig->TextFontHeight == 8)
  1805.                 {
  1806.                         /* Looks like you lose. */
  1807.  
  1808.                     ReleaseDefaultPubScreen();
  1809.  
  1810.                     return(LocaleString(MSG_TERMINIT_UNABLE_TO_OPEN_TEXT_TXT));
  1811.                 }
  1812.                 else
  1813.                 {
  1814.                         /* Use the ROM default font. */
  1815.  
  1816.                     strcpy(Config->TerminalConfig->TextFontName,"topaz.font");
  1817.                     Config->TerminalConfig->TextFontHeight = 8;
  1818.                 }
  1819.             }
  1820.         }
  1821.     }
  1822.     while(TextFont == NULL);
  1823.  
  1824.     TextFontHeight    = TextFont->tf_YSize;
  1825.     TextFontWidth    = TextFont->tf_XSize;
  1826.     TextFontBase    = TextFont->tf_Baseline;
  1827.  
  1828.         /* Determine extra font box width for slanted/boldface glyphs. */
  1829.  
  1830.     FontRightExtend    = MAX(TextFont->tf_XSize / 2,TextFont->tf_BoldSmear);
  1831.  
  1832.     CurrentFont = TextFont;
  1833.  
  1834.     GFXFont.ta_YSize = TextFontHeight;
  1835.  
  1836.     if(GFX = SmartOpenDiskFont(&GFXFont))
  1837.     {
  1838.         if(GFX->tf_XSize != TextFont->tf_XSize || GFX->tf_YSize != TextFont->tf_YSize)
  1839.         {
  1840.             CloseFont(GFX);
  1841.  
  1842.             GFX = NULL;
  1843.         }
  1844.     }
  1845.  
  1846.     UserFontHeight    = UserTextFont->tf_YSize;
  1847.     UserFontWidth    = UserTextFont->tf_XSize;
  1848.     UserFontBase    = UserTextFont->tf_Baseline;
  1849.  
  1850.         /* We will need this one later */
  1851.  
  1852.     OpenWindowTag = WA_CustomScreen;
  1853.  
  1854.     if(Config->ScreenConfig->UseWorkbench || Config->ScreenConfig->ShareScreen)
  1855.     {
  1856.         LONG WindowLeft,WindowTop,DummyWidth,DummyHeight;
  1857.         LONG FullWidth,Height,Width,Index;
  1858.         struct ViewPortExtra *Extra;
  1859.         struct TagItem SomeTags[7];
  1860.         struct Screen *LocalScreen;
  1861.         BOOL SimpleRefresh;
  1862.  
  1863.         Index = 0;
  1864.         LocalScreen = DefaultPubScreen;
  1865.         WindowLeft = WindowTop = DummyWidth = DummyHeight = -1;
  1866.  
  1867.         if(Config->ScreenConfig->ShareScreen && !Config->ScreenConfig->UseWorkbench)
  1868.         {
  1869.             struct DimensionInfo DimensionInfo;
  1870.  
  1871.             if(ModeNotAvailable(Config->ScreenConfig->DisplayMode))
  1872.             {
  1873.                 Config->ScreenConfig->DisplayMode = GetVPModeID(&DefaultPubScreen->ViewPort);
  1874.  
  1875.                 if(GetDisplayInfoData(NULL,(APTR)&DimensionInfo,sizeof(struct DimensionInfo),DTAG_DIMS,Config->ScreenConfig->DisplayMode))
  1876.                 {
  1877.                     LONG Width,Height;
  1878.  
  1879.                     Width    = DimensionInfo.TxtOScan.MaxX - DimensionInfo.TxtOScan.MinX + 1;
  1880.                     Height    = DimensionInfo.TxtOScan.MaxY - DimensionInfo.TxtOScan.MinY + 1;
  1881.  
  1882.                     if(Width != Config->ScreenConfig->DisplayWidth && Config->ScreenConfig->DisplayWidth)
  1883.                         Config->ScreenConfig->DisplayWidth = Width;
  1884.  
  1885.                     if(Height != Config->ScreenConfig->DisplayHeight && Config->ScreenConfig->DisplayHeight)
  1886.                         Config->ScreenConfig->DisplayHeight = Height;
  1887.                 }
  1888.             }
  1889.  
  1890.             if(GetDisplayInfoData(NULL,(APTR)&DimensionInfo,sizeof(struct DimensionInfo),DTAG_DIMS,Config->ScreenConfig->DisplayMode))
  1891.             {
  1892.                 LONG Depth,ScreenWidth,ScreenHeight;
  1893.                 UWORD Pens;
  1894.  
  1895.                 Pens = (UWORD)~0;
  1896.  
  1897.                 if(Config->ScreenConfig->DisplayWidth && Config->ScreenConfig->DisplayHeight && AslBase->lib_Version >= 38)
  1898.                 {
  1899.                     ScreenWidth        = Config->ScreenConfig->DisplayWidth;
  1900.                     ScreenHeight    = Config->ScreenConfig->DisplayHeight;
  1901.                 }
  1902.                 else
  1903.                 {
  1904.                     ScreenWidth        = 0;
  1905.                     ScreenHeight    = 0;
  1906.                 }
  1907.  
  1908.                 switch(Config->ScreenConfig->ColourMode)
  1909.                 {
  1910.                     case COLOUR_EIGHT:
  1911.  
  1912.                         Depth = 3 + 1;
  1913.                         break;
  1914.  
  1915.                     case COLOUR_SIXTEEN:
  1916.  
  1917.                         Depth = 4 + 1;
  1918.                         break;
  1919.  
  1920.                     case COLOUR_AMIGA:
  1921.  
  1922.                         Depth = 2;
  1923.                         break;
  1924.  
  1925.                     default:
  1926.  
  1927.                         Depth = 1;
  1928.                         break;
  1929.                 }
  1930.  
  1931.                 if(Depth > DimensionInfo.MaxDepth)
  1932.                     Depth = DimensionInfo.MaxDepth;
  1933.  
  1934.                 if(!Kick30 && Depth > 2)
  1935.                     Depth = 2;
  1936.  
  1937.                 if(Config->ScreenConfig->Depth && Config->ScreenConfig->Depth <= DimensionInfo.MaxDepth)
  1938.                     Depth = Config->ScreenConfig->Depth;
  1939.  
  1940.                 switch(Config->ScreenConfig->ColourMode)
  1941.                 {
  1942.                     case COLOUR_EIGHT:
  1943.  
  1944.                         if(Depth < 3 + 1)
  1945.                             Config->ScreenConfig->ColourMode = COLOUR_AMIGA;
  1946.  
  1947.                         break;
  1948.  
  1949.                     case COLOUR_SIXTEEN:
  1950.  
  1951.                         if(Depth < 4 + 1)
  1952.                         {
  1953.                             if(Depth < 3 + 1)
  1954.                                 Config->ScreenConfig->ColourMode = COLOUR_AMIGA;
  1955.                             else
  1956.                                 Config->ScreenConfig->ColourMode = COLOUR_EIGHT;
  1957.                         }
  1958.  
  1959.                         break;
  1960.                 }
  1961.  
  1962.                 LimitedSPrintf(sizeof(ScreenTitle),ScreenTitle,LocaleString(MSG_TERMINIT_SCREENTITLE_TXT),TermName,Machine,TermDate,TermIDString);
  1963.  
  1964.                 StatusSizeSetup(NULL,&StatusWidth,&StatusHeight);
  1965.  
  1966.                 if(StatusHeight && StatusWidth > ScreenWidth)
  1967.                     ScreenWidth = StatusWidth;
  1968.  
  1969.                 if(SharedScreen = OpenScreenTags(NULL,
  1970.                     ScreenWidth  ? SA_Width  : TAG_IGNORE,    ScreenWidth,
  1971.                     ScreenHeight ? SA_Height : TAG_IGNORE,    ScreenHeight,
  1972.  
  1973.                     SA_Title,        ScreenTitle,
  1974.                     SA_Depth,        Depth,
  1975.                     SA_Pens,        &Pens,
  1976.                     SA_Overscan,    AslBase->lib_Version >= 38 ? Config->ScreenConfig->OverscanType : OSCAN_TEXT,
  1977.                     SA_DisplayID,    Config->ScreenConfig->DisplayMode,
  1978.                     SA_Font,        &UserFont,
  1979.                     SA_AutoScroll,    TRUE,
  1980.                     SA_ShowTitle,    Config->ScreenConfig->TitleBar,
  1981.                     SA_PubName,        TermIDString,
  1982.                     SA_Interleaved,    TRUE,
  1983.                     SA_SharePens,    TRUE,
  1984.                 TAG_DONE))
  1985.                 {
  1986.                     LocalScreen = SharedScreen;
  1987.  
  1988.                     if(Config->ScreenConfig->MakeScreenPublic)
  1989.                         PubScreenStatus(LocalScreen,NULL);
  1990.                     else
  1991.                         PubScreenStatus(LocalScreen,PSNF_PRIVATE);
  1992.                 }
  1993.             }
  1994.         }
  1995.  
  1996.         if(!SharedScreen)
  1997.             LimitedSPrintf(sizeof(ScreenTitle),ScreenTitle,"%s %s(%s)",TermName,Machine,TermDate);
  1998.  
  1999.         StatusSizeSetup(LocalScreen,&StatusWidth,&StatusHeight);
  2000.  
  2001.         UseMasking = ChooseMasking(LocalScreen);
  2002.  
  2003.         VPort = &LocalScreen->ViewPort;
  2004.  
  2005.             /* Get the current display dimensions. */
  2006.  
  2007.         if(Extra = GetViewPortExtra(VPort))
  2008.         {
  2009.             ScreenWidth        = Extra->DisplayClip.MaxX - Extra->DisplayClip.MinX + 1;
  2010.             ScreenHeight    = Extra->DisplayClip.MaxY - Extra->DisplayClip.MinY + 1;
  2011.         }
  2012.         else
  2013.         {
  2014.             ScreenWidth        = LocalScreen->Width;
  2015.             ScreenHeight    = LocalScreen->Height;
  2016.         }
  2017.  
  2018.         DepthMask = (1L << GetBitMapDepth(LocalScreen->RastPort.BitMap)) - 1;
  2019.  
  2020.         switch(Config->ScreenConfig->ColourMode)
  2021.         {
  2022.             case COLOUR_SIXTEEN:
  2023.  
  2024.                 if(DepthMask < 15)
  2025.                 {
  2026.                     if(DepthMask >= 7)
  2027.                         Config->ScreenConfig->ColourMode = COLOUR_EIGHT;
  2028.                     else
  2029.                     {
  2030.                         if(DepthMask >= 3)
  2031.                             Config->ScreenConfig->ColourMode = COLOUR_AMIGA;
  2032.                         else
  2033.                             Config->ScreenConfig->ColourMode = COLOUR_MONO;
  2034.                     }
  2035.                 }
  2036.  
  2037.                 break;
  2038.  
  2039.             case COLOUR_EIGHT:
  2040.  
  2041.                 if(DepthMask < 7)
  2042.                 {
  2043.                     if(DepthMask >= 3)
  2044.                         Config->ScreenConfig->ColourMode = COLOUR_AMIGA;
  2045.                     else
  2046.                         Config->ScreenConfig->ColourMode = COLOUR_MONO;
  2047.                 }
  2048.  
  2049.                 break;
  2050.  
  2051.             case COLOUR_AMIGA:
  2052.  
  2053.                 if(DepthMask < 3)
  2054.                     Config->ScreenConfig->ColourMode = COLOUR_MONO;
  2055.  
  2056.                 break;
  2057.         }
  2058.  
  2059.         if(!(DrawInfo = GetScreenDrawInfo(LocalScreen)))
  2060.         {
  2061.             ReleaseDefaultPubScreen();
  2062.  
  2063.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OBTAIN_SCREEN_DRAWINFO_TXT));
  2064.         }
  2065.         else
  2066.             Pens = DrawInfo->dri_Pens;
  2067.  
  2068.         CreateMenuGlyphs(LocalScreen,DrawInfo,&AmigaGlyph,&CheckGlyph);
  2069.  
  2070.         SZ_SizeSetup(LocalScreen,(struct TextAttr *)&UserFont);
  2071.  
  2072.             /* Obtain visual info (whatever that may be). */
  2073.  
  2074.         if(!(VisualInfo = GetVisualInfo(LocalScreen,TAG_DONE)))
  2075.         {
  2076.                 /* Delete the DrawInfo now, or it won't
  2077.                  * get freed during shutdown.
  2078.                  */
  2079.  
  2080.             FreeScreenDrawInfo(LocalScreen,DrawInfo);
  2081.             DrawInfo = NULL;
  2082.  
  2083.             ReleaseDefaultPubScreen();
  2084.  
  2085.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OBTAIN_VISUAL_INFO_TXT));
  2086.         }
  2087.  
  2088.         if(Config->ScreenConfig->StatusLine != STATUSLINE_DISABLED && !Config->ScreenConfig->SplitStatus)
  2089.             FullWidth = StatusWidth;
  2090.         else
  2091.             FullWidth = 0;
  2092.  
  2093.         GetWindowInfo(WINDOW_MAIN,&WindowLeft,&WindowTop,&DummyWidth,&DummyHeight,0,0);
  2094.  
  2095.         if(UsePresetSize)
  2096.         {
  2097.             SomeTags[Index  ].ti_Tag    = WA_Width;
  2098.             SomeTags[Index++].ti_Data    = DummyWidth;
  2099.  
  2100.             SomeTags[Index  ].ti_Tag    = WA_Height;
  2101.             SomeTags[Index++].ti_Data    = DummyHeight;
  2102.         }
  2103.         else
  2104.         {
  2105.             if(Config->TerminalConfig->NumColumns < 20)
  2106.             {
  2107.                 LONG Width = GetScreenWidth(NULL);
  2108.  
  2109.                 if(FullWidth && Width < FullWidth)
  2110.                 {
  2111.                     SomeTags[Index  ].ti_Tag    = WA_InnerWidth;
  2112.                     SomeTags[Index++].ti_Data    = FullWidth;
  2113.                 }
  2114.                 else
  2115.                 {
  2116.                     SomeTags[Index  ].ti_Tag    = WA_Width;
  2117.                     SomeTags[Index++].ti_Data    = Width;
  2118.                 }
  2119.             }
  2120.             else
  2121.             {
  2122.                 SomeTags[Index  ].ti_Tag    = WA_InnerWidth;
  2123.                 SomeTags[Index++].ti_Data    = Config->TerminalConfig->NumColumns * TextFontWidth;
  2124.             }
  2125.  
  2126.             if(Config->TerminalConfig->NumLines < 20)
  2127.             {
  2128.                 SomeTags[Index  ].ti_Tag    = WA_Height;
  2129.                 SomeTags[Index++].ti_Data    = GetScreenHeight(NULL) - (LocalScreen->BarHeight + 1);
  2130.             }
  2131.             else
  2132.             {
  2133.                 SomeTags[Index  ].ti_Tag    = WA_InnerHeight;
  2134.                 SomeTags[Index++].ti_Data    = Config->TerminalConfig->NumLines * TextFontHeight + StatusHeight;
  2135.             }
  2136.         }
  2137.  
  2138.         if(WindowLeft != -1)
  2139.         {
  2140.             SomeTags[Index  ].ti_Tag    = WA_Left;
  2141.             SomeTags[Index++].ti_Data    = WindowLeft;
  2142.         }
  2143.         else
  2144.         {
  2145.             SomeTags[Index  ].ti_Tag    = WA_Left;
  2146.             SomeTags[Index++].ti_Data    = GetScreenLeft(NULL);
  2147.         }
  2148.  
  2149.         if(WindowTop != -1)
  2150.         {
  2151.             SomeTags[Index  ].ti_Tag    = WA_Top;
  2152.             SomeTags[Index++].ti_Data    = WindowTop;
  2153.         }
  2154.  
  2155.         SomeTags[Index].ti_Tag = TAG_DONE;
  2156.  
  2157.             /* If we're using a public screen, take care of it. */
  2158.  
  2159.         if(!SharedScreen)
  2160.             OpenWindowTag = WA_PubScreen;
  2161.  
  2162.             /* How smart have we got to be? */
  2163.  
  2164.         SimpleRefresh = SimpleRefreshAllowed(Config);
  2165.  
  2166.             /* Open the main window. */
  2167.  
  2168.         if(!(Window = OpenWindowTags(NULL,
  2169.             WA_MaxHeight,        LocalScreen->Height,
  2170.             WA_MaxWidth,        LocalScreen->Width,
  2171.             WA_NewLookMenus,    TRUE,
  2172.             WA_RMBTrap,            TRUE,
  2173.             WA_IDCMP,            DEFAULT_IDCMP | IDCMP_SIZEVERIFY | IDCMP_REFRESHWINDOW,
  2174.             WA_DragBar,            TRUE,
  2175.             WA_DepthGadget,        TRUE,
  2176.             WA_CloseGadget,        TRUE,
  2177.             WA_SizeGadget,        TRUE,
  2178.             WA_SizeBBottom,        Config->ScreenConfig->StatusLine == STATUSLINE_DISABLED || Config->ScreenConfig->SplitStatus,
  2179.             WA_Title,            WindowTitle[0] ? WindowTitle : ScreenTitle,
  2180.             WA_MenuHelp,        TRUE,
  2181.             WA_Activate,        Activate,
  2182.             OpenWindowTag,        LocalScreen,
  2183.  
  2184.             WA_SmartRefresh,    !SimpleRefresh,
  2185.             WA_NoCareRefresh,    !SimpleRefresh,
  2186.             WA_SimpleRefresh,    SimpleRefresh,
  2187.  
  2188.             AmigaGlyph ? WA_AmigaKey  : TAG_IGNORE, AmigaGlyph,
  2189.             CheckGlyph ? WA_Checkmark : TAG_IGNORE, CheckGlyph,
  2190.  
  2191.         TAG_MORE,SomeTags)))
  2192.         {
  2193.                 /* Delete the DrawInfo now, or it won't
  2194.                  * get freed during shutdown.
  2195.                  */
  2196.  
  2197.             FreeScreenDrawInfo(LocalScreen,DrawInfo);
  2198.             DrawInfo = NULL;
  2199.  
  2200.             ReleaseDefaultPubScreen();
  2201.  
  2202.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_WINDOW_TXT));
  2203.         }
  2204.  
  2205.         if(WindowTitle[0])
  2206.             SetWindowTitles(Window,(STRPTR)-1,ScreenTitle);
  2207.  
  2208.         if(StatusHeight)
  2209.         {
  2210.             StatusDisplayHeight = StatusHeight;
  2211.  
  2212.             CopyMem(Window->RPort,StatusRPort = &StatusRastPort,sizeof(struct RastPort));
  2213.         }
  2214.         else
  2215.             StatusRPort = NULL;
  2216.  
  2217.             /* Create a user clip region to keep text from
  2218.              * leaking into the window borders.
  2219.              */
  2220.  
  2221.         if(!(ClipRegion = NewRegion()))
  2222.         {
  2223.             ReleaseDefaultPubScreen();
  2224.  
  2225.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_WINDOW_TXT));
  2226.         }
  2227.         else
  2228.         {
  2229.             struct Rectangle RegionRectangle;
  2230.  
  2231.                 /* Adjust the region to match the inner window area. */
  2232.  
  2233.             RegionRectangle.MinX = Window->BorderLeft;
  2234.             RegionRectangle.MinY = Window->BorderTop;
  2235.             RegionRectangle.MaxX = Window->Width - (Window->BorderRight + 1);
  2236.             RegionRectangle.MaxY = Window->Height - (Window->BorderBottom + 1);
  2237.  
  2238.                 /* Establish the region. */
  2239.  
  2240.             OrRectRegion(ClipRegion,&RegionRectangle);
  2241.  
  2242.                 /* Install the region. */
  2243.  
  2244.             OldRegion = InstallClipRegion(Window->WLayer,ClipRegion);
  2245.         }
  2246.  
  2247.         if(FullWidth < 40 * TextFontWidth)
  2248.             FullWidth = 40 * TextFontWidth;
  2249.  
  2250.         Width    = Window->BorderLeft + FullWidth + Window->BorderRight;
  2251.         Height    = Window->BorderTop + 20 * TextFontHeight + Window->BorderBottom + StatusHeight;
  2252.  
  2253.         if(ChatMode && !(Config->TerminalConfig->EmulationMode == EMULATION_EXTERNAL && Config->TerminalConfig->EmulationFileName[0]))
  2254.             Height += UserFontHeight + 2;
  2255.  
  2256.         WindowLimits(Window,Width,Height,0,0);
  2257.  
  2258.         ReleaseDefaultPubScreen();
  2259.     }
  2260.     else
  2261.     {
  2262.         struct DimensionInfo DimensionInfo;
  2263.         LONG MaxDepth,ScreenDepth;
  2264.         BOOL SimpleRefresh;
  2265.  
  2266.         if(ModeNotAvailable(Config->ScreenConfig->DisplayMode))
  2267.         {
  2268.             struct Screen *PubScreen;
  2269.  
  2270.             if(PubScreen = LockPubScreen(NULL))
  2271.             {
  2272.                 Config->ScreenConfig->DisplayMode = GetVPModeID(&PubScreen->ViewPort);
  2273.  
  2274.                 if(GetDisplayInfoData(NULL,(APTR)&DimensionInfo,sizeof(struct DimensionInfo),DTAG_DIMS,Config->ScreenConfig->DisplayMode))
  2275.                 {
  2276.                     LONG Width,Height;
  2277.  
  2278.                     Width    = DimensionInfo.TxtOScan.MaxX - DimensionInfo.TxtOScan.MinX + 1;
  2279.                     Height    = DimensionInfo.TxtOScan.MaxY - DimensionInfo.TxtOScan.MinY + 1;
  2280.  
  2281.                     if(Width != Config->ScreenConfig->DisplayWidth && Config->ScreenConfig->DisplayWidth)
  2282.                         Config->ScreenConfig->DisplayWidth = Width;
  2283.  
  2284.                     if(Height != Config->ScreenConfig->DisplayHeight && Config->ScreenConfig->DisplayHeight)
  2285.                         Config->ScreenConfig->DisplayHeight = Height;
  2286.                 }
  2287.  
  2288.                 UnlockPubScreen(NULL,PubScreen);
  2289.             }
  2290.         }
  2291.  
  2292.             /* Query the display size. If it fails, back out immediately. */
  2293.  
  2294.         if(!QueryOverscan(Config->ScreenConfig->DisplayMode,&DisplayClip,Config->ScreenConfig->OverscanType))
  2295.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_SCREEN_TXT));
  2296.  
  2297.         if(GetDisplayInfoData(NULL,(APTR)&DimensionInfo,sizeof(struct DimensionInfo),DTAG_DIMS,Config->ScreenConfig->DisplayMode))
  2298.         {
  2299.             LONG MaxWidth,MaxHeight,Width,Height;
  2300.  
  2301.             MaxWidth    = DisplayClip.MaxX - DisplayClip.MinX + 1;
  2302.             MaxHeight    = DisplayClip.MaxY - DisplayClip.MinY + 1;
  2303.  
  2304.             if(Config->ScreenConfig->DisplayWidth && Config->ScreenConfig->DisplayHeight && AslBase->lib_Version >= 38)
  2305.             {
  2306.                 ScreenWidth        = Config->ScreenConfig->DisplayWidth;
  2307.                 ScreenHeight    = Config->ScreenConfig->DisplayHeight;
  2308.             }
  2309.             else
  2310.             {
  2311.                 ScreenWidth        = MaxWidth;
  2312.                 ScreenHeight    = MaxHeight;
  2313.             }
  2314.  
  2315.             if(Config->TerminalConfig->NumColumns < 20)
  2316.                 Width = MaxWidth = ScreenWidth;
  2317.             else
  2318.             {
  2319.                 Width = TextFontWidth * Config->TerminalConfig->NumColumns;
  2320.  
  2321.                 ScreenWidth = 0;
  2322.             }
  2323.  
  2324.             if(Config->TerminalConfig->NumLines < 20)
  2325.                 Height = MaxHeight = ScreenHeight;
  2326.             else
  2327.             {
  2328.                 Height = TextFontHeight * Config->TerminalConfig->NumLines;
  2329.  
  2330.                 if(Config->ScreenConfig->TitleBar)
  2331.                     Height += UserFontHeight + 3;
  2332.  
  2333.                 if(Config->ScreenConfig->StatusLine != STATUSLINE_DISABLED)
  2334.                 {
  2335.                     if(Config->ScreenConfig->StatusLine == STATUSLINE_COMPRESSED)
  2336.                         Height += UserFontHeight;
  2337.                     else
  2338.                         Height += 4 + (2 + 2 * UserFontHeight + 2);
  2339.                 }
  2340.  
  2341.                 ScreenHeight = 0;
  2342.             }
  2343.  
  2344.             if(Height > MaxHeight)
  2345.                 Height = MaxHeight;
  2346.  
  2347.             if(Width > MaxWidth)
  2348.                 Width = MaxWidth;
  2349.  
  2350.             if(DimensionInfo.MinRasterWidth <= Width && Width <= DimensionInfo.MaxRasterWidth && Width < MaxWidth)
  2351.             {
  2352.                 LONG Half;
  2353.  
  2354.                 Width = MaxWidth - Width;
  2355.  
  2356.                 Half = Width / 2;
  2357.  
  2358.                 DisplayClip.MinX += Half;
  2359.                 DisplayClip.MaxX -= Width - Half;
  2360.             }
  2361.  
  2362.             if(DimensionInfo.MinRasterHeight <= Height && Height <= DimensionInfo.MaxRasterHeight)
  2363.                 DisplayClip.MaxY = DisplayClip.MinY + Height - 1;
  2364.  
  2365.             if(!ScreenWidth)
  2366.                 ScreenWidth = DisplayClip.MaxX - DisplayClip.MinX + 1;
  2367.  
  2368.             if(!ScreenHeight)
  2369.                 ScreenHeight = DisplayClip.MaxY - DisplayClip.MinY + 1;
  2370.  
  2371.             MaxDepth = DimensionInfo.MaxDepth;
  2372.         }
  2373.         else
  2374.         {
  2375.             ScreenWidth = ScreenHeight = 0;
  2376.             MaxDepth = 4;
  2377.         }
  2378.  
  2379.             /* We'll configure the screen parameters at
  2380.              * run time, at first we'll set up the screen
  2381.              * depth.
  2382.              */
  2383.  
  2384.         do
  2385.         {
  2386.             RethinkPens = FALSE;
  2387.  
  2388.             if(!Config->ScreenConfig->UsePens && Kick30)
  2389.             {
  2390.                 for(i = DETAILPEN ; i <= BARTRIMPEN ; i++)
  2391.                     PenArray[i] = Config->ScreenConfig->PenArray[i];
  2392.  
  2393.                 PenArray[i] = (UWORD)~0;
  2394.             }
  2395.             else
  2396.             {
  2397.                 UWORD *Data;
  2398.  
  2399.                 switch(Config->ScreenConfig->ColourMode)
  2400.                 {
  2401.                     case COLOUR_EIGHT:
  2402.  
  2403.                         Data = ANSIPens;
  2404.                         break;
  2405.  
  2406.                     case COLOUR_SIXTEEN:
  2407.  
  2408.                         if(Kick30)
  2409.                             Data = NewEGAPens;
  2410.                         else
  2411.                             Data = EGAPens;
  2412.  
  2413.                         break;
  2414.  
  2415.                     case COLOUR_AMIGA:
  2416.  
  2417.                         Data = StandardPens;
  2418.                         break;
  2419.  
  2420.                     default:
  2421.  
  2422.                         Data = NULL;
  2423.                         break;
  2424.                 }
  2425.  
  2426.                 if(Data)
  2427.                 {
  2428.                     for(i = DETAILPEN ; i <= BARTRIMPEN ; i++)
  2429.                         PenArray[i] = Data[i];
  2430.  
  2431.                     PenArray[i] = (UWORD)~0;
  2432.                 }
  2433.             }
  2434.  
  2435.             switch(Config->ScreenConfig->ColourMode)
  2436.             {
  2437.                 case COLOUR_EIGHT:
  2438.  
  2439.                         /* Special screen depth requested? */
  2440.  
  2441.                     if(Config->ScreenConfig->Depth)
  2442.                     {
  2443.                             /* The minimum number of colours required */
  2444.  
  2445.                         if(Config->ScreenConfig->Blinking)
  2446.                             ScreenDepth = 4;
  2447.                         else
  2448.                             ScreenDepth = 3;
  2449.  
  2450.                             /* This is what the user wanted */
  2451.  
  2452.                         RealDepth = Config->ScreenConfig->Depth;
  2453.  
  2454.                             /* Too deep for this display mode? */
  2455.  
  2456.                         if(RealDepth > MaxDepth)
  2457.                             RealDepth = MaxDepth;
  2458.  
  2459.                             /* Less colours than required? */
  2460.  
  2461.                         if(RealDepth < ScreenDepth && ScreenDepth <= MaxDepth)
  2462.                             RealDepth = ScreenDepth;
  2463.  
  2464.                             /* Not enough colours to display it? */
  2465.  
  2466.                         if(RealDepth < ScreenDepth)
  2467.                         {
  2468.                                 /* Return to standard mode */
  2469.  
  2470.                             Config->ScreenConfig->ColourMode = COLOUR_AMIGA;
  2471.  
  2472.                             ConfigChanged = TRUE;
  2473.  
  2474.                             RethinkPens = TRUE;
  2475.  
  2476.                             break;
  2477.                         }
  2478.                     }
  2479.                     else
  2480.                     {
  2481.                             /* The minimum number of colours */
  2482.  
  2483.                         if(Config->ScreenConfig->Blinking)
  2484.                             ScreenDepth = 4;
  2485.                         else
  2486.                             ScreenDepth = 3;
  2487.  
  2488.                             /* Too many for this mode? */
  2489.  
  2490.                         if(ScreenDepth > MaxDepth)
  2491.                         {
  2492.                                 /* Return to standard mode */
  2493.  
  2494.                             Config->ScreenConfig->ColourMode = COLOUR_AMIGA;
  2495.  
  2496.                             ConfigChanged = TRUE;
  2497.                             RethinkPens = TRUE;
  2498.  
  2499.                             break;
  2500.                         }
  2501.  
  2502.                         RealDepth = ScreenDepth;
  2503.                     }
  2504.  
  2505.                     TagArray[Count++] = SA_Pens;
  2506.                     TagArray[Count++] = (LONG)PenArray;
  2507.  
  2508.                     TagArray[Count++] = SA_BlockPen;
  2509.                     TagArray[Count++] = PenArray[SHADOWPEN];
  2510.  
  2511.                     TagArray[Count++] = SA_DetailPen;
  2512.                     TagArray[Count++] = PenArray[BACKGROUNDPEN];
  2513.  
  2514.                     break;
  2515.  
  2516.                 case COLOUR_SIXTEEN:
  2517.  
  2518.                     if(Config->ScreenConfig->Depth)
  2519.                     {
  2520.                         if(Config->ScreenConfig->Blinking && MaxDepth > 4)
  2521.                             ScreenDepth = 5;
  2522.                         else
  2523.                             ScreenDepth = 4;
  2524.  
  2525.                         RealDepth = Config->ScreenConfig->Depth;
  2526.  
  2527.                         if(RealDepth > MaxDepth)
  2528.                             RealDepth = MaxDepth;
  2529.  
  2530.                         if(RealDepth < ScreenDepth && ScreenDepth <= MaxDepth)
  2531.                             RealDepth = ScreenDepth;
  2532.  
  2533.                         if(RealDepth < ScreenDepth)
  2534.                         {
  2535.                             Config->ScreenConfig->ColourMode = COLOUR_AMIGA;
  2536.  
  2537.                             ConfigChanged = TRUE;
  2538.                             RethinkPens = TRUE;
  2539.  
  2540.                             break;
  2541.                         }
  2542.                     }
  2543.                     else
  2544.                     {
  2545.                         if(Config->ScreenConfig->Blinking && MaxDepth > 4)
  2546.                             ScreenDepth = 5;
  2547.                         else
  2548.                             ScreenDepth = 4;
  2549.  
  2550.                         if(ScreenDepth > MaxDepth)
  2551.                         {
  2552.                             Config->ScreenConfig->ColourMode = COLOUR_AMIGA;
  2553.  
  2554.                             ConfigChanged = TRUE;
  2555.                             RethinkPens = TRUE;
  2556.  
  2557.                             break;
  2558.                         }
  2559.  
  2560.                         RealDepth = ScreenDepth;
  2561.                     }
  2562.  
  2563.                     TagArray[Count++] = SA_Pens;
  2564.                     TagArray[Count++] = (LONG)PenArray;
  2565.  
  2566.                     TagArray[Count++] = SA_BlockPen;
  2567.                     TagArray[Count++] = PenArray[SHADOWPEN];
  2568.  
  2569.                     TagArray[Count++] = SA_DetailPen;
  2570.                     TagArray[Count++] = PenArray[BACKGROUNDPEN];
  2571.  
  2572.                     break;
  2573.  
  2574.                 case COLOUR_MONO:
  2575.  
  2576.                     if(Config->ScreenConfig->Depth)
  2577.                         RealDepth = Config->ScreenConfig->Depth;
  2578.                     else
  2579.                         RealDepth = 1;
  2580.  
  2581.                     if(RealDepth > MaxDepth)
  2582.                         RealDepth = MaxDepth;
  2583.  
  2584.                     break;
  2585.  
  2586.                 case COLOUR_AMIGA:
  2587.  
  2588.                     if(Config->ScreenConfig->Depth)
  2589.                         RealDepth = Config->ScreenConfig->Depth;
  2590.                     else
  2591.                         RealDepth = 2;
  2592.  
  2593.                     if(RealDepth > MaxDepth)
  2594.                         RealDepth = MaxDepth;
  2595.  
  2596.                     TagArray[Count++] = SA_Pens;
  2597.                     TagArray[Count++] = (LONG)PenArray;
  2598.  
  2599.                     TagArray[Count++] = SA_BlockPen;
  2600.                     TagArray[Count++] = PenArray[SHADOWPEN];
  2601.  
  2602.                     TagArray[Count++] = SA_DetailPen;
  2603.                     TagArray[Count++] = PenArray[BACKGROUNDPEN];
  2604.  
  2605.                     break;
  2606.             }
  2607.         }
  2608.         while(RethinkPens);
  2609.  
  2610.             /* Add the depth value. */
  2611.  
  2612.         TagArray[Count++] = SA_Depth;
  2613.         TagArray[Count++] = RealDepth;
  2614.  
  2615.             /* Terminate the tag array. */
  2616.  
  2617.         TagArray[Count] = TAG_END;
  2618.  
  2619.             /* Set the plane mask. */
  2620.  
  2621.         DepthMask = (1L << RealDepth) - 1;
  2622.  
  2623.         LimitedSPrintf(sizeof(ScreenTitle),ScreenTitle,LocaleString(MSG_TERMINIT_SCREENTITLE_TXT),TermName,Machine,TermDate,TermIDString);
  2624.  
  2625.         StatusSizeSetup(NULL,&StatusWidth,&StatusHeight);
  2626.  
  2627.         if(StatusHeight && StatusWidth > ScreenWidth)
  2628.             ScreenWidth = StatusWidth;
  2629.  
  2630.         do
  2631.         {
  2632.             Screen = (struct Screen *)OpenScreenTags(NULL,
  2633.                 ScreenWidth  ? SA_Width  : TAG_IGNORE,    ScreenWidth,
  2634.                 ScreenHeight ? SA_Height : TAG_IGNORE,    ScreenHeight,
  2635.  
  2636.                 SA_Title,        ScreenTitle,
  2637.                 SA_Left,        DisplayClip.MinX,
  2638.                 SA_DClip,        &DisplayClip,
  2639.                 SA_DisplayID,    Config->ScreenConfig->DisplayMode,
  2640.                 SA_Font,        &UserFont,
  2641.                 SA_Behind,        !Activate,
  2642.                 SA_AutoScroll,    TRUE,
  2643.                 SA_ShowTitle,    Config->ScreenConfig->TitleBar,
  2644.                 SA_PubName,        TermIDString,
  2645.                 SA_ErrorCode,    &ErrorCode,
  2646.                 SA_Interleaved,    TRUE,
  2647.                 SA_BackFill,    &BackfillHook,
  2648.             TAG_MORE,TagArray);
  2649.  
  2650.             if(Screen != NULL)
  2651.             {
  2652.                 UseMasking = ChooseMasking(Screen);
  2653.             }
  2654.             else
  2655.             {
  2656.                     /* So we've got an error. */
  2657.  
  2658.                 if(OpenFailed)
  2659.                     return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_SCREEN_TXT));
  2660.                 else
  2661.                 {
  2662.                     struct Screen *PubScreen;
  2663.  
  2664.                     switch(ErrorCode)
  2665.                     {
  2666.                             /* Can't open screen with these display
  2667.                              * modes.
  2668.                              */
  2669.  
  2670.                         case OSERR_NOMONITOR:
  2671.                         case OSERR_NOCHIPS:
  2672.                         case OSERR_UNKNOWNMODE:
  2673.                         case OSERR_NOTAVAILABLE:
  2674.  
  2675.                             if(PubScreen = LockPubScreen(NULL))
  2676.                             {
  2677.                                 struct DimensionInfo DimensionInfo;
  2678.  
  2679.                                 Config->ScreenConfig->DisplayMode = GetVPModeID(&PubScreen->ViewPort);
  2680.  
  2681.                                 if(GetDisplayInfoData(NULL,(APTR)&DimensionInfo,sizeof(struct DimensionInfo),DTAG_DIMS,Config->ScreenConfig->DisplayMode))
  2682.                                 {
  2683.                                     LONG Width,Height;
  2684.  
  2685.                                     Width    = DimensionInfo.TxtOScan.MaxX - DimensionInfo.TxtOScan.MinX + 1;
  2686.                                     Height    = DimensionInfo.TxtOScan.MaxY - DimensionInfo.TxtOScan.MinY + 1;
  2687.  
  2688.                                     if(Width != Config->ScreenConfig->DisplayWidth && Config->ScreenConfig->DisplayWidth)
  2689.                                         Config->ScreenConfig->DisplayWidth = Width;
  2690.  
  2691.                                     if(Height != Config->ScreenConfig->DisplayHeight && Config->ScreenConfig->DisplayHeight)
  2692.                                         Config->ScreenConfig->DisplayHeight = Height;
  2693.                                 }
  2694.  
  2695.                                 UnlockPubScreen(NULL,PubScreen);
  2696.                             }
  2697.                             else
  2698.                                 Config->ScreenConfig->DisplayMode = DEFAULT_MONITOR_ID | HIRESLACE_KEY;
  2699.  
  2700.                             OpenFailed = TRUE;
  2701.  
  2702.                             break;
  2703.  
  2704.                         case OSERR_PUBNOTUNIQUE:
  2705.  
  2706.                             return(LocaleString(MSG_TERMINIT_SCREEN_ID_ALREADY_IN_USE_TXT));
  2707.  
  2708.                             /* Some different error, probably out of
  2709.                              * memory.
  2710.                              */
  2711.  
  2712.                         default:
  2713.  
  2714.                             return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_SCREEN_TXT));
  2715.                     }
  2716.                 }
  2717.             }
  2718.         }
  2719.         while(Screen == NULL);
  2720.  
  2721.         if(!(DrawInfo = GetScreenDrawInfo(Screen)))
  2722.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OBTAIN_SCREEN_DRAWINFO_TXT));
  2723.         else
  2724.             Pens = DrawInfo->dri_Pens;
  2725.  
  2726.         CreateMenuGlyphs(Screen,DrawInfo,&AmigaGlyph,&CheckGlyph);
  2727.  
  2728.         VPort = &Screen->ViewPort;
  2729.  
  2730.         ScreenWidth        = Screen->Width;
  2731.         ScreenHeight    = Screen->Height;
  2732.  
  2733.         StatusSizeSetup(Screen,&StatusWidth,&StatusHeight);
  2734.  
  2735.             /* Obtain visual info (whatever that may be). */
  2736.  
  2737.         if(!(VisualInfo = GetVisualInfo(Screen,TAG_DONE)))
  2738.         {
  2739.                 /* Delete the DrawInfo now, or it won't
  2740.                  * get freed during shutdown.
  2741.                  */
  2742.  
  2743.             FreeScreenDrawInfo(Screen,DrawInfo);
  2744.  
  2745.             DrawInfo = NULL;
  2746.  
  2747.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OBTAIN_VISUAL_INFO_TXT));
  2748.         }
  2749.  
  2750.         if(Config->ScreenConfig->TitleBar)
  2751.         {
  2752.             Top = Screen->BarHeight + 1;
  2753.  
  2754.             Height = Screen->Height - (Screen->BarHeight + 1);
  2755.         }
  2756.         else
  2757.         {
  2758.             Top = 0;
  2759.  
  2760.             Height = Screen->Height;
  2761.         }
  2762.  
  2763.             /* How smart have we got to be? */
  2764.  
  2765.         SimpleRefresh = SimpleRefreshAllowed(Config);
  2766.  
  2767.             /* Open the main window. */
  2768.  
  2769.         if(!(Window = OpenWindowTags(NULL,
  2770.             WA_Top,                Top,
  2771.             WA_Left,            0,
  2772.             WA_Width,            Screen->Width,
  2773.             WA_Height,            Height,
  2774.             WA_Backdrop,        TRUE,
  2775.             WA_Borderless,        TRUE,
  2776.             WA_CustomScreen,    Screen,
  2777.             WA_NewLookMenus,    TRUE,
  2778.             WA_RMBTrap,            TRUE,
  2779.             WA_IDCMP,            (DEFAULT_IDCMP & ~IDCMP_CLOSEWINDOW) | IDCMP_REFRESHWINDOW,
  2780.             WA_MenuHelp,        TRUE,
  2781.             WA_Activate,        Activate,
  2782.  
  2783.             WA_SmartRefresh,    !SimpleRefresh,
  2784.             WA_NoCareRefresh,    !SimpleRefresh,
  2785.             WA_SimpleRefresh,    SimpleRefresh,
  2786.  
  2787.             AmigaGlyph ? WA_AmigaKey  : TAG_IGNORE, AmigaGlyph,
  2788.             CheckGlyph ? WA_Checkmark : TAG_IGNORE, CheckGlyph,
  2789.         TAG_DONE)))
  2790.         {
  2791.                 /* Delete the DrawInfo now, or it won't
  2792.                  * get freed during shutdown.
  2793.                  */
  2794.  
  2795.             FreeScreenDrawInfo(Screen,DrawInfo);
  2796.  
  2797.             DrawInfo = NULL;
  2798.  
  2799.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_WINDOW_TXT));
  2800.         }
  2801.  
  2802.         if(StatusHeight)
  2803.         {
  2804.             StatusDisplayHeight = StatusHeight;
  2805.  
  2806.             CopyMem(Window->RPort,StatusRPort = &StatusRastPort,sizeof(struct RastPort));
  2807.         }
  2808.         else
  2809.             StatusRPort = NULL;
  2810.     }
  2811.  
  2812.         /* Now check if a nonzero background colour should be used. */
  2813.  
  2814.     if(Pens[BACKGROUNDPEN])
  2815.         BackfillTag = WA_BackFill;
  2816.     else
  2817.         BackfillTag = TAG_IGNORE;
  2818.  
  2819.         /* Fill the `default' colour with current values. */
  2820.  
  2821.     if(!Config->ScreenConfig->UseWorkbench && Initializing && !SharedScreen)
  2822.     {
  2823.         if(Kick30)
  2824.         {
  2825.             ULONG RGB[3 * 16];
  2826.  
  2827.             GetRGB32(VPort->ColorMap,0,16,RGB);
  2828.  
  2829.             ANSIColourTable        = CreateColourTable(8,    ANSIColours,    NULL);
  2830.             EGAColourTable        = CreateColourTable(16,    EGAColours,    NULL);
  2831.             DefaultColourTable    = CreateColourTable(16,    NULL,        RGB);
  2832.             MonoColourTable        = CreateColourTable(2,    AtomicColours,    NULL);
  2833.         }
  2834.  
  2835.         for(i = 0 ; i < 16 ; i++)
  2836.             DefaultColours[i] = GetRGB4(VPort->ColorMap,i);
  2837.  
  2838.         Initializing = FALSE;
  2839.     }
  2840.  
  2841.         /* Load the approriate colours. */
  2842.  
  2843.     if(LoadColours)
  2844.     {
  2845.         Default2CurrentPalette(Config);
  2846.  
  2847.         LoadColours = FALSE;
  2848.     }
  2849.  
  2850.         /* Reset the current colours and the blinking equivalents. */
  2851.  
  2852.     PaletteSetup(Config);
  2853.  
  2854.     if(!Config->ScreenConfig->UseWorkbench && !SharedScreen)
  2855.         LoadColourTable(VPort,NormalColourTable,NormalColours,PaletteSize);
  2856.  
  2857.         /* Get the vanilla rendering pens. */
  2858.  
  2859.     RenderPens[0] = Pens[BACKGROUNDPEN];
  2860.     RenderPens[1] = Pens[TEXTPEN];
  2861.     RenderPens[2] = Pens[SHINEPEN];
  2862.     RenderPens[3] = Pens[FILLPEN];
  2863.  
  2864.         /* Are we to use the Workbench screen for text output? */
  2865.  
  2866.     if(Config->ScreenConfig->UseWorkbench || (SharedScreen && Kick30))
  2867.     {
  2868.         BOOL UseDefaults;
  2869.  
  2870.         if(Kick30 && (Config->ScreenConfig->ColourMode == COLOUR_EIGHT || Config->ScreenConfig->ColourMode == COLOUR_SIXTEEN))
  2871.         {
  2872.             LONG NumPens;
  2873.             ULONG R,G,B;
  2874.             BOOL GotAll;
  2875.  
  2876.             UseDefaults = FALSE;
  2877.  
  2878.             do
  2879.             {
  2880.                 if(Config->ScreenConfig->ColourMode == COLOUR_EIGHT)
  2881.                     NumPens = 8;
  2882.                 else
  2883.                     NumPens = 16;
  2884.  
  2885.                 for(i = 0 ; i < 32 ; i++)
  2886.                     MappedPens[1][i] = FALSE;
  2887.  
  2888.                 GotAll = TRUE;
  2889.  
  2890.                     /* Allocate the text rendering pens, note that
  2891.                      * we will use the currently installed palette
  2892.                      * to obtain those pens which match them best.
  2893.                      * The user will be unable to change these
  2894.                      * colours.
  2895.                      */
  2896.  
  2897.                 if(NormalColourTable)
  2898.                 {
  2899.                     for(i = 0 ; i < NumPens ; i++)
  2900.                     {
  2901.                             /* Try to obtain a matching pen. */
  2902.  
  2903.                         if((MappedPens[0][i] = ObtainBestPen(VPort->ColorMap,NormalColourTable->Entry[i].Red,NormalColourTable->Entry[i].Green,NormalColourTable->Entry[i].Blue,
  2904.                             OBP_FailIfBad,TRUE,
  2905.                         TAG_DONE)) == -1)
  2906.                         {
  2907.                             MappedPens[1][i] = FALSE;
  2908.  
  2909.                             GotAll = FALSE;
  2910.  
  2911.                             break;
  2912.                         }
  2913.                         else
  2914.                             MappedPens[1][i] = TRUE;
  2915.                     }
  2916.                 }
  2917.                 else
  2918.                 {
  2919.                     for(i = 0 ; i < NumPens ; i++)
  2920.                     {
  2921.                             /* Split the 12 bit colour palette entry. */
  2922.  
  2923.                         R = (NormalColours[i] >> 8) & 0xF;
  2924.                         G = (NormalColours[i] >> 4) & 0xF;
  2925.                         B =  NormalColours[i]       & 0xF;
  2926.  
  2927.                             /* Try to obtain a matching pen. */
  2928.  
  2929.                         if((MappedPens[0][i] = ObtainBestPen(VPort->ColorMap,SPREAD((R << 4) | R),SPREAD((G << 4) | G),SPREAD((B << 4) | B),
  2930.                             OBP_FailIfBad,TRUE,
  2931.                         TAG_DONE)) == -1)
  2932.                         {
  2933.                             MappedPens[1][i] = FALSE;
  2934.  
  2935.                             GotAll = FALSE;
  2936.  
  2937.                             break;
  2938.                         }
  2939.                         else
  2940.                             MappedPens[1][i] = TRUE;
  2941.                     }
  2942.                 }
  2943.  
  2944.                     /* Did we get what we wanted? */
  2945.  
  2946.                 if(!GotAll)
  2947.                 {
  2948.                         /* Release all the pens we succeeded
  2949.                          * in allocating.
  2950.                          */
  2951.  
  2952.                     for(i = 0 ; i < NumPens ; i++)
  2953.                     {
  2954.                         if(MappedPens[1][i])
  2955.                             ReleasePen(VPort->ColorMap,MappedPens[0][i]);
  2956.                     }
  2957.  
  2958.                         /* Check if we could step back */
  2959.  
  2960.                     if(Config->ScreenConfig->ColourMode == COLOUR_SIXTEEN)
  2961.                         Config->ScreenConfig->ColourMode = COLOUR_EIGHT;
  2962.                     else
  2963.                     {
  2964.                         Config->ScreenConfig->ColourMode = COLOUR_AMIGA;
  2965.  
  2966.                         UseDefaults = TRUE;
  2967.                     }
  2968.                 }
  2969.                 else
  2970.                     AllocatedPens = TRUE;
  2971.             }
  2972.             while(!AllocatedPens && !UseDefaults);
  2973.         }
  2974.         else
  2975.             UseDefaults = TRUE;
  2976.  
  2977.             /* Use the default rendering pens? */
  2978.  
  2979.         if(UseDefaults)
  2980.         {
  2981.             if(Config->ScreenConfig->ColourMode == COLOUR_AMIGA)
  2982.             {
  2983.                 for(i = 0 ; i < 4 ; i++)
  2984.                 {
  2985.                     MappedPens[0][i] = RenderPens[i];
  2986.                     MappedPens[1][i] = FALSE;
  2987.                 }
  2988.  
  2989.                     /* Set the remaining pens to defaults. */
  2990.  
  2991.                 for(i = 4 ; i < 16 ; i++)
  2992.                 {
  2993.                     MappedPens[0][i] = RenderPens[1];
  2994.                     MappedPens[1][i] = FALSE;
  2995.                 }
  2996.             }
  2997.             else
  2998.             {
  2999.                 for(i = 0 ; i < 16 ; i++)
  3000.                 {
  3001.                     MappedPens[0][i] = RenderPens[i & 1];
  3002.                     MappedPens[1][i] = FALSE;
  3003.                 }
  3004.             }
  3005.         }
  3006.  
  3007.         for(i = 0 ; i < 16 ; i++)
  3008.         {
  3009.             MappedPens[0][i + 16] = MappedPens[0][i];
  3010.             MappedPens[1][i + 16] = FALSE;
  3011.         }
  3012.     }
  3013.     else
  3014.     {
  3015.             /* Reset the colour translation table. */
  3016.  
  3017.         for(i = 0 ; i < 32 ; i++)
  3018.         {
  3019.             MappedPens[0][i] = i;
  3020.             MappedPens[1][i] = FALSE;
  3021.         }
  3022.     }
  3023.  
  3024.         /* Determine default text rendering colour. */
  3025.  
  3026.     switch(Config->ScreenConfig->ColourMode)
  3027.     {
  3028.         case COLOUR_SIXTEEN:
  3029.  
  3030.             SafeTextPen = MappedPens[0][7];
  3031.             break;
  3032.  
  3033.         case COLOUR_EIGHT:
  3034.  
  3035.             SafeTextPen = MappedPens[0][7];
  3036.             break;
  3037.  
  3038.         case COLOUR_AMIGA:
  3039.         case COLOUR_MONO:
  3040.  
  3041.             SafeTextPen = MappedPens[0][1];
  3042.             break;
  3043.     }
  3044.  
  3045.         /* Take care of pen and attribute mapping. */
  3046.  
  3047.     if(Config->EmulationConfig->UseStandardPens)
  3048.     {
  3049.         for(i = 0 ; i < 16 ; i++)
  3050.         {
  3051.             PenTable[i]        = i;
  3052.             TextAttributeTable[i]    = i;
  3053.         }
  3054.     }
  3055.     else
  3056.     {
  3057.         UBYTE Value;
  3058.  
  3059.         CopyMem(Config->EmulationConfig->Pens,PenTable,sizeof(Config->EmulationConfig->Pens));
  3060.  
  3061.         for(i = 0 ; i < 16 ; i++)
  3062.         {
  3063.             Value = 0;
  3064.  
  3065.             if(i & ATTR_UNDERLINE)
  3066.             {
  3067.                 switch(Config->EmulationConfig->Attributes[TEXTATTR_UNDERLINE])
  3068.                 {
  3069.                     case TEXTATTR_UNDERLINE:
  3070.  
  3071.                         Value |= ATTR_UNDERLINE;
  3072.                         break;
  3073.  
  3074.                     case TEXTATTR_HIGHLIGHT:
  3075.  
  3076.                         Value |= ATTR_HIGHLIGHT;
  3077.                         break;
  3078.  
  3079.                     case TEXTATTR_BLINK:
  3080.  
  3081.                         Value |= ATTR_BLINK;
  3082.                         break;
  3083.  
  3084.                     case TEXTATTR_INVERSE:
  3085.  
  3086.                         Value |= ATTR_INVERSE;
  3087.                         break;
  3088.                 }
  3089.             }
  3090.  
  3091.             if(i & ATTR_HIGHLIGHT)
  3092.             {
  3093.                 switch(Config->EmulationConfig->Attributes[TEXTATTR_HIGHLIGHT])
  3094.                 {
  3095.                     case TEXTATTR_UNDERLINE:
  3096.  
  3097.                         Value |= ATTR_UNDERLINE;
  3098.                         break;
  3099.  
  3100.                     case TEXTATTR_HIGHLIGHT:
  3101.  
  3102.                         Value |= ATTR_HIGHLIGHT;
  3103.                         break;
  3104.  
  3105.                     case TEXTATTR_BLINK:
  3106.  
  3107.                         Value |= ATTR_BLINK;
  3108.                         break;
  3109.  
  3110.                     case TEXTATTR_INVERSE:
  3111.  
  3112.                         Value |= ATTR_INVERSE;
  3113.                         break;
  3114.                 }
  3115.             }
  3116.  
  3117.             if(i & ATTR_BLINK)
  3118.             {
  3119.                 switch(Config->EmulationConfig->Attributes[TEXTATTR_BLINK])
  3120.                 {
  3121.                     case TEXTATTR_UNDERLINE:
  3122.  
  3123.                         Value |= ATTR_UNDERLINE;
  3124.                         break;
  3125.  
  3126.                     case TEXTATTR_HIGHLIGHT:
  3127.  
  3128.                         Value |= ATTR_HIGHLIGHT;
  3129.                         break;
  3130.  
  3131.                     case TEXTATTR_BLINK:
  3132.  
  3133.                         Value |= ATTR_BLINK;
  3134.                         break;
  3135.  
  3136.                     case TEXTATTR_INVERSE:
  3137.  
  3138.                         Value |= ATTR_INVERSE;
  3139.                         break;
  3140.                 }
  3141.             }
  3142.  
  3143.             if(i & ATTR_INVERSE)
  3144.             {
  3145.                 switch(Config->EmulationConfig->Attributes[TEXTATTR_INVERSE])
  3146.                 {
  3147.                     case TEXTATTR_UNDERLINE:
  3148.  
  3149.                         Value |= ATTR_UNDERLINE;
  3150.                         break;
  3151.  
  3152.                     case TEXTATTR_HIGHLIGHT:
  3153.  
  3154.                         Value |= ATTR_HIGHLIGHT;
  3155.                         break;
  3156.  
  3157.                     case TEXTATTR_BLINK:
  3158.  
  3159.                         Value |= ATTR_BLINK;
  3160.                         break;
  3161.  
  3162.                     case TEXTATTR_INVERSE:
  3163.  
  3164.                         Value |= ATTR_INVERSE;
  3165.                         break;
  3166.                 }
  3167.             }
  3168.  
  3169.             TextAttributeTable[i] = Value;
  3170.         }
  3171.     }
  3172.  
  3173.         /* Determine window inner dimensions and top/left edge offsets. */
  3174.  
  3175.     UpdateTerminalLimits();
  3176.  
  3177.         /* Set up scaling data (bitmaps & rastports). */
  3178.  
  3179.     if(!CreateScale(Window))
  3180.         return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_FONT_SCALING_INFO_TXT));
  3181.  
  3182.     if(!CreateOffsetTables())
  3183.         return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_OFFSET_TABLES_TXT));
  3184.  
  3185.     TabStopMax = Window->WScreen->Width / TextFontWidth;
  3186.  
  3187.         /* Allocate the tab stop line. */
  3188.  
  3189.     if(!(TabStops = (BYTE *)AllocVecPooled(TabStopMax,MEMF_ANY | MEMF_CLEAR)))
  3190.         return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  3191.  
  3192.         /* Reset the Window job handler to the default routine. */
  3193.  
  3194.     WindowJob = SingleWindowJob;
  3195.  
  3196.         /* Push it on the window stack (should become bottommost
  3197.          * entry).
  3198.          */
  3199.  
  3200.     PushWindow(Window);
  3201.  
  3202.     if(TermPort)
  3203.         TermPort->TopWindow = Window;
  3204.  
  3205.     if(Config->ScreenConfig->StatusLine != STATUSLINE_DISABLED && Config->ScreenConfig->SplitStatus)
  3206.     {
  3207.         if(!(StatusWindow = OpenWindowTags(NULL,
  3208.             WA_Top,                Window->TopEdge + Window->Height,
  3209.             WA_Left,            Window->LeftEdge,
  3210.             WA_InnerWidth,        StatusWidth,
  3211.             WA_InnerHeight,        StatusHeight,
  3212.             WA_DragBar,            TRUE,
  3213.             WA_DepthGadget,        TRUE,
  3214.             WA_NewLookMenus,    TRUE,
  3215.             WA_Title,            ScreenTitle,
  3216.             OpenWindowTag,        Window->WScreen,
  3217.             WA_RMBTrap,            TRUE,
  3218.             WA_CloseGadget,        TRUE,
  3219.             WA_MenuHelp,        TRUE,
  3220.             WA_NoCareRefresh,    TRUE,
  3221.  
  3222.             AmigaGlyph ? WA_AmigaKey  : TAG_IGNORE, AmigaGlyph,
  3223.             CheckGlyph ? WA_Checkmark : TAG_IGNORE, CheckGlyph,
  3224.         TAG_DONE)))
  3225.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_STATUS_WINDOW_TXT));
  3226.     }
  3227.     else
  3228.         StatusWindow = NULL;
  3229.  
  3230.     if(StatusWindow)
  3231.     {
  3232.         StatusWindow->UserPort = Window->UserPort;
  3233.  
  3234.         if(!ModifyIDCMP(StatusWindow,DEFAULT_IDCMP))
  3235.         {
  3236.             StatusWindow->UserPort = NULL;
  3237.  
  3238.             CloseWindow(StatusWindow);
  3239.             StatusWindow = NULL;
  3240.  
  3241.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_STATUS_WINDOW_TXT));
  3242.         }
  3243.     }
  3244.  
  3245.     UpdateTerminalLimits();
  3246.  
  3247.     RPort = Window->RPort;
  3248.  
  3249.         /* Default console setup. */
  3250.  
  3251.     CursorX = 0;
  3252.     CursorY    = 0;
  3253.  
  3254.     SetDrMd(RPort,JAM2);
  3255.  
  3256.         /* Set the font. */
  3257.  
  3258.     SetFont(RPort,CurrentFont);
  3259.  
  3260.         /* Redirect AmigaDOS requesters. */
  3261.  
  3262.     ChangeWindowPtr(&OldWindowPtr,(APTR)Window);
  3263.  
  3264.         /* Create the character raster. */
  3265.  
  3266.     if(!CreateRaster())
  3267.         return(LocaleString(MSG_TERMINIT_UNABLE_TO_CREATE_SCREEN_RASTER_TXT));
  3268.  
  3269.     ConOutputUpdate();
  3270.  
  3271.     ConFontScaleUpdate();
  3272.  
  3273.         /* Set up the scrolling info. */
  3274.  
  3275.     ScrollLineCount = Window->WScreen->Height / TextFontHeight;
  3276.  
  3277.     if(!(ScrollLines = (struct ScrollLineInfo *)AllocVecPooled(sizeof(struct ScrollLineInfo) * ScrollLineCount,MEMF_ANY|MEMF_CLEAR)))
  3278.         return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_SCROLLING_SUPPORT_INFO_TXT));
  3279.  
  3280.         /* Create the menu strip. */
  3281.  
  3282.     if(!AttachMenu(NULL))
  3283.         return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_MENUS_TXT));
  3284.  
  3285.         /* Disable the `Execute ARexx Command' menu item if
  3286.          * the rexx server is not available.
  3287.          */
  3288.  
  3289.     if(!RexxSysBase)
  3290.         OffItem(MEN_EXECUTE_REXX_COMMAND);
  3291.  
  3292.     if(Recording)
  3293.     {
  3294.         OnItem(MEN_RECORD_LINE);
  3295.  
  3296.         CheckItem(MEN_RECORD,TRUE);
  3297.         CheckItem(MEN_RECORD_LINE,RecordingLine);
  3298.     }
  3299.     else
  3300.     {
  3301.         OffItem(MEN_RECORD_LINE);
  3302.  
  3303.         CheckItem(MEN_RECORD,FALSE);
  3304.         CheckItem(MEN_RECORD_LINE,FALSE);
  3305.     }
  3306.  
  3307.     CheckItem(MEN_DISABLE_TRAPS,!(WatchTraps && !IsListEmpty((struct List *)GenericListTable[GLIST_TRAP])));
  3308.  
  3309.     if(StatusWindow)
  3310.     {
  3311.         SetMenuStrip(StatusWindow,Menu);
  3312.  
  3313.         StatusWindow->Flags &= ~WFLG_RMBTRAP;
  3314.  
  3315.         SetDrMd(StatusWindow->RPort,JAM2);
  3316.     }
  3317.  
  3318.         /* Add a tick if file capture is active. */
  3319.  
  3320.     if(RawCapture)
  3321.     {
  3322.         if(FileCapture)
  3323.             CheckItem(MEN_CAPTURE_TO_RAW_FILE,TRUE);
  3324.         else
  3325.             CheckItem(MEN_CAPTURE_TO_RAW_FILE,FALSE);
  3326.  
  3327.         CheckItem(MEN_CAPTURE_TO_FILE,FALSE);
  3328.     }
  3329.     else
  3330.     {
  3331.         if(FileCapture)
  3332.             CheckItem(MEN_CAPTURE_TO_FILE,TRUE);
  3333.         else
  3334.             CheckItem(MEN_CAPTURE_TO_FILE,FALSE);
  3335.     }
  3336.  
  3337.         /* Add a tick if printer capture is active. */
  3338.  
  3339.     CheckItem(MEN_CAPTURE_TO_PRINTER,PrinterCapture != NULL);
  3340.  
  3341.         /* Add a tick if the buffer is frozen. */
  3342.  
  3343.     CheckItem(MEN_FREEZE_BUFFER,BufferFrozen);
  3344.  
  3345.         /* Disable the dialing functions if online. */
  3346.  
  3347.     if(Online)
  3348.         SetDialMenu(FALSE);
  3349.     else
  3350.         SetDialMenu(TRUE);
  3351.  
  3352.         /* Take care of the chat line. */
  3353.  
  3354.     if(Config->TerminalConfig->EmulationMode == EMULATION_EXTERNAL && Config->TerminalConfig->EmulationFileName[0])
  3355.         OffItem(MEN_CHAT_LINE);
  3356.     else
  3357.         CheckItem(MEN_CHAT_LINE,ChatMode);
  3358.  
  3359.         /* Update the clipboard menus. */
  3360.  
  3361.     SetClipMenu(FALSE);
  3362.  
  3363.     SetTransferMenu(TRUE);
  3364.  
  3365.         /* Disable the `Print Screen' and `Save ASCII' functions
  3366.          * if raster is not enabled.
  3367.          */
  3368.  
  3369.     SetRasterMenu(RasterEnabled);
  3370.  
  3371.         /* Take care of the remaining windows */
  3372.  
  3373.     if(MatrixWindow)
  3374.         CheckItem(MEN_MATRIX_WINDOW,TRUE);
  3375.  
  3376.     if(InfoWindow)
  3377.         CheckItem(MEN_STATUS_WINDOW,TRUE);
  3378.  
  3379.     if(PacketWindow)
  3380.         CheckItem(MEN_PACKET_WINDOW,TRUE);
  3381.  
  3382.     if(ChatMode)
  3383.         CheckItem(MEN_CHAT_LINE,TRUE);
  3384.  
  3385.     if(FastWindow)
  3386.         CheckItem(MEN_FAST_MACROS_WINDOW,TRUE);
  3387.  
  3388.         /* Enable the menu. */
  3389.  
  3390.     Window->Flags &= ~WFLG_RMBTRAP;
  3391.  
  3392.     strcpy(EmulationName,LocaleString(MSG_TERMXEM_NO_EMULATION_TXT));
  3393.  
  3394.         /* Create the status server. */
  3395.  
  3396.     Forbid();
  3397.  
  3398.     if(StatusProcess = CreateNewProcTags(
  3399.         NP_Entry,        StatusServer,
  3400.         NP_Name,        "term Status Process",
  3401.         NP_WindowPtr,    -1,
  3402.         NP_Priority,    5,
  3403.     TAG_DONE))
  3404.         WaitForHandshake();
  3405.  
  3406.     Permit();
  3407.  
  3408.     if(!StatusProcess)
  3409.         return(LocaleString(MSG_TERMINIT_UNABLE_TO_CREATE_STATUS_TASK_TXT));
  3410.  
  3411.         /* Obtain the default public screen name just in case
  3412.          * we'll need it later.
  3413.          */
  3414.  
  3415.     GetDefaultPubScreen(DefaultPubScreenName);
  3416.  
  3417.         /* Set up the window size. */
  3418.  
  3419.     ScreenSizeStuff();
  3420.  
  3421.         /* Select the default console data processing routine. */
  3422.  
  3423.     ConProcessUpdate();
  3424.  
  3425.         /* Handle the remaining terminal setup. */
  3426.  
  3427.     if(Config->TerminalConfig->EmulationMode == EMULATION_EXTERNAL && Config->TerminalConfig->EmulationFileName[0])
  3428.     {
  3429.         if(!OpenEmulator(Config->TerminalConfig->EmulationFileName))
  3430.         {
  3431.             Config->TerminalConfig->EmulationMode = EMULATION_ANSIVT100;
  3432.  
  3433.             ResetDisplay = TRUE;
  3434.             ActivateJob(MainJobQueue,ResetDisplayJob);
  3435.  
  3436.             RasterEnabled = TRUE;
  3437.  
  3438.             ShowRequest(Window,LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_EMULATION_LIBRARY_TXT),Config->TerminalConfig->EmulationFileName);
  3439.         }
  3440.         else
  3441.         {
  3442.             if(RasterEnabled)
  3443.                 RasterEnabled = FALSE;
  3444.  
  3445.             SetRasterMenu(RasterEnabled);
  3446.         }
  3447.     }
  3448.  
  3449.         /* Choose the right console data processing routine. */
  3450.  
  3451.     ConProcessUpdate();
  3452.  
  3453.         /* Reset terminal emulation. */
  3454.  
  3455.     if(Config->TerminalConfig->EmulationMode != EMULATION_EXTERNAL)
  3456.     {
  3457.         ClearCursor();
  3458.  
  3459.         ForegroundPen = GetPenIndex(SafeTextPen);
  3460.         BackgroundPen = 0;
  3461.  
  3462.         UpdatePens(RPort);
  3463.  
  3464.         Reset();
  3465.  
  3466.         DrawCursor();
  3467.     }
  3468.     else
  3469.     {
  3470.         if(XEmulatorBase)
  3471.             XEmulatorResetConsole(XEM_IO);
  3472.     }
  3473.  
  3474.         /* Restart the fast! macro panel. */
  3475.  
  3476.     if(HadFastMacros || Config->MiscConfig->OpenFastMacroPanel)
  3477.         OpenFastWindow();
  3478.  
  3479.     TTYResize();
  3480.  
  3481.     UpdateJob(MainJobQueue,WindowJob,SIG_WINDOW);
  3482.  
  3483.     return(NULL);
  3484. }
  3485.  
  3486.     /* CloseAll():
  3487.      *
  3488.      *    Free all resources and leave the program.
  3489.      */
  3490.  
  3491. VOID
  3492. CloseAll()
  3493. {
  3494.     RemoveRendezvousSemaphore();
  3495.  
  3496.     DispatchRexxDialMsgList(FALSE);
  3497.  
  3498.     DeleteRecord();
  3499.  
  3500.     DeleteQueueProcess();
  3501.  
  3502.     SoundExit();
  3503.  
  3504.     SZ_SizeCleanup();
  3505.  
  3506.     if(IntuitionBase && Window)
  3507.         BlockWindows();
  3508.  
  3509.     if(RexxPort)
  3510.         RemPort(RexxPort);
  3511.  
  3512.     DeleteChatGadget();
  3513.  
  3514.     if(TermRexxPort)
  3515.     {
  3516.         if(RexxSysBase)
  3517.         {
  3518.             struct Message *Msg;
  3519.  
  3520.             while(Msg = GetMsg(TermRexxPort))
  3521.                 ReplyMsg(Msg);
  3522.         }
  3523.  
  3524.         DeleteMsgPort(TermRexxPort);
  3525.     }
  3526.  
  3527.     ShakeHands((struct Task *)RexxProcess,SIG_KILL);
  3528.  
  3529.     CloseLibrary((struct Library *)RexxSysBase);
  3530.  
  3531.     if(XprIO && XProtocolBase)
  3532.         XProtocolCleanup(XprIO);
  3533.  
  3534.     CloseLibrary(XProtocolBase);
  3535.  
  3536.     FreeVec(XprIO);
  3537.  
  3538.     TerminateBuffer();
  3539.  
  3540.     DeleteSpeech();
  3541.  
  3542.     Forbid();
  3543.  
  3544.     BufferClosed = TRUE;
  3545.  
  3546.     DeleteBuffer();
  3547.  
  3548.     Permit();
  3549.  
  3550.     if(FileCapture)
  3551.     {
  3552.         BufferClose(FileCapture);
  3553.  
  3554.         if(!GetFileSize(CaptureName))
  3555.             DeleteFile(CaptureName);
  3556.         else
  3557.         {
  3558.             AddProtection(CaptureName,FIBF_EXECUTE);
  3559.  
  3560.             if(Config->MiscConfig->CreateIcons)
  3561.                 AddIcon(CaptureName,FILETYPE_TEXT,FALSE);
  3562.         }
  3563.     }
  3564.  
  3565.     if(PrinterCapture)
  3566.         Close(PrinterCapture);
  3567.  
  3568.         /* Close the external emulator. */
  3569.  
  3570.     CloseEmulator(TRUE);
  3571.  
  3572.     if(IntuitionBase && GfxBase)
  3573.         DeleteDisplay();
  3574.  
  3575.     CaptureParserExit();
  3576.  
  3577.     UnLoadSeg(KeySegment);
  3578.  
  3579.     StopCall(TRUE);
  3580.  
  3581.     DeleteAccountant();
  3582.  
  3583.     FreeSignal(CheckBit);
  3584.  
  3585.     ClearSerial();
  3586.  
  3587.     DeleteSerial();
  3588.  
  3589.     StopTime();
  3590.  
  3591.     DeleteTimeRequest(TimeRequest,TimePort);
  3592.  
  3593.     ShutdownCx();
  3594.  
  3595.     if(TermPort)
  3596.     {
  3597.         if(TermID != -1)
  3598.         {
  3599.             ObtainSemaphore(&TermPort->OpenSemaphore);
  3600.  
  3601.             TermPort->OpenCount--;
  3602.  
  3603.             if(TermPort->OpenCount <= 0 && !TermPort->HoldIt)
  3604.             {
  3605.                 RemPort(&TermPort->MsgPort);
  3606.  
  3607.                 ReleaseSemaphore(&TermPort->OpenSemaphore);
  3608.  
  3609.                 FreeVec(TermPort);
  3610.             }
  3611.             else
  3612.                 ReleaseSemaphore(&TermPort->OpenSemaphore);
  3613.         }
  3614.     }
  3615.  
  3616.     FreeSignal(OwnDevBit);
  3617.     CloseLibrary(OwnDevUnitBase);
  3618.  
  3619.     CloseClip();
  3620.  
  3621.     LocaleClose();
  3622.  
  3623.     DeleteMsgQueue(SpecialQueue);
  3624.  
  3625.     if(ConsoleDevice)
  3626.         CloseDevice((struct IORequest *)ConsoleRequest);
  3627.  
  3628.     CloseLibrary(GTLayoutBase);
  3629.  
  3630.     CloseLibrary(IconBase);
  3631.  
  3632.     CloseLibrary(DataTypesBase);
  3633.  
  3634.     CloseLibrary(WorkbenchBase);
  3635.  
  3636.     CloseLibrary(CxBase);
  3637.  
  3638.     CloseLibrary(IFFParseBase);
  3639.  
  3640.     CloseLibrary(AslBase);
  3641.  
  3642.     CloseLibrary(GadToolsBase);
  3643.  
  3644.     CloseLibrary(LayersBase);
  3645.  
  3646.     CloseLibrary((struct Library *)GfxBase);
  3647.  
  3648.     CloseLibrary((struct Library *)IntuitionBase);
  3649.  
  3650.     MemoryCleanup();
  3651. }
  3652.  
  3653.     /* OpenAll():
  3654.      *
  3655.      *    Open all required resources or return an error message
  3656.      *    if anything went wrong.
  3657.      */
  3658.  
  3659. STRPTR
  3660. OpenAll(STRPTR ConfigPath)
  3661. {
  3662.     UBYTE PathBuffer[MAX_FILENAME_LENGTH];
  3663.     STRPTR Result,ConfigFileName;
  3664.     UBYTE LocalBuffer[256];
  3665.     LONG ErrorCode,i;
  3666.     BOOL ZapPhonebook;
  3667.  
  3668.     ConfigFileName = NULL;
  3669.  
  3670.     Kick30 = (SysBase->LibNode.lib_Version >= 39);
  3671.  
  3672.         /* These are here in order to avoid trouble later */
  3673.  
  3674.     ConTransferUpdate();
  3675.     SerWriteUpdate();
  3676.  
  3677.         /* Remember the start of this session. */
  3678.  
  3679.     DateStamp(&SessionStart);
  3680.  
  3681.         /* Set up the hooks in Data.c */
  3682.  
  3683.     InitHook(&LocaleHook,(HOOKFUNC)LocaleHookFunc,NULL);
  3684.     InitHook(&GuideHook,(HOOKFUNC)GuideSetupHook,NULL);
  3685.     InitHook(&BackfillHook,(HOOKFUNC)BackfillRoutine,NULL);
  3686.  
  3687.         /* Set up font DPI tag list. */
  3688.  
  3689.     TagDPI[0].ti_Tag = TAG_DONE;
  3690.  
  3691.         /* Initialize all the semaphores */
  3692.  
  3693.     for(i = 0 ; StartupSemaphoreTable[i] != NULL ; i++)
  3694.         InitSemaphore(StartupSemaphoreTable[i]);
  3695.  
  3696.         /* Initialize all the lists */
  3697.  
  3698.     for(i = 0 ; StartupListTable[i] != NULL ; i++)
  3699.         NewList(StartupListTable[i]);
  3700.  
  3701.         /* And this is for the quick dial menu. */
  3702.  
  3703.     for(i = 0 ; i < DIAL_MENU_MAX; i++)
  3704.         QuickDialTable[i] = -1;
  3705.  
  3706.     QuickDialMax = 0;
  3707.  
  3708.     if(!MemorySetup())
  3709.         return("Cannot create memory pool");
  3710.  
  3711.         /* Rendezvous setup. */
  3712.  
  3713.     RendezvousSetup();
  3714.  
  3715.         /* Set up the program launcher. */
  3716.  
  3717.     LaunchSetup();
  3718.  
  3719.         /* Open the translation tables. */
  3720.  
  3721.     LocaleOpen("term.catalog","english",20);
  3722.  
  3723.         /* Fill in the menu configuration. */
  3724.  
  3725.     PrepareLocalizedMenu(&TermMenu,&NumMenuEntries);
  3726.  
  3727.         /* Open intuition.library, any version. */
  3728.  
  3729.     if(!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0)))
  3730.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_INTUITION_LIBRARY_TXT));
  3731.  
  3732.         /* Check if we should use the old style sliders. */
  3733.  
  3734.     if(GetVar("termoldsliders",PathBuffer,sizeof(PathBuffer),NULL) >= 0)
  3735.         SliderType = SLIDER_KIND;
  3736.     else
  3737.         SliderType = LEVEL_KIND;
  3738.  
  3739.         /* Open some more libraries. */
  3740.  
  3741.     if(!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0)))
  3742.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_GRAPHICS_LIBRARY_TXT));
  3743.  
  3744.     if(!(LayersBase = OpenLibrary("layers.library",0)))
  3745.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_LAYERS_LIBRARY_TXT));
  3746.  
  3747.         /* Install the correct routines to query
  3748.          * the rendering colours and drawing mode.
  3749.          */
  3750.  
  3751.     if(!Kick30)
  3752.     {
  3753.         ReadAPen = OldGetAPen;
  3754.         ReadBPen = OldGetBPen;
  3755.         ReadDrMd = OldGetDrMd;
  3756.         SetMask = OldSetWrMsk;
  3757.         GetMask = OldGetWrMsk;
  3758.     }
  3759.     else
  3760.     {
  3761.         ReadAPen = NewGetAPen;
  3762.         ReadBPen = NewGetBPen;
  3763.         ReadDrMd = NewGetDrMd;
  3764.         SetMask = NewSetWrMsk;
  3765.         GetMask = NewGetWrMsk;
  3766.     }
  3767.  
  3768.         /* Check if locale.library has already installed the operating system
  3769.          * patches required for localization.
  3770.          */
  3771.  
  3772.     LanguageCheck();
  3773.  
  3774.         /* Open the remaining libraries. */
  3775.  
  3776.     if(!(GadToolsBase = OpenLibrary("gadtools.library",0)))
  3777.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_GADTOOLS_LIBRARY_TXT));
  3778.  
  3779.     if(!(AslBase = OpenLibrary("asl.library",0)))
  3780.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_ASL_LIBRARY_TXT));
  3781.  
  3782.     if(!(IFFParseBase = OpenLibrary("iffparse.library",0)))
  3783.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_IFFPARSE_LIBRARY_TXT));
  3784.  
  3785.     if(!(CxBase = OpenLibrary("commodities.library",0)))
  3786.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_COMMODITIES_LIBRARY_TXT));
  3787.  
  3788.     if(!CreateTimeRequest(UNIT_VBLANK,&TimeRequest,&TimePort))
  3789.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_TIMER_DEVICE_TXT));
  3790.  
  3791.     TimerBase = TimeRequest->tr_node.io_Device;
  3792.  
  3793.         /* User interface. */
  3794.  
  3795.     if(!(GTLayoutBase = SafeOpenLibrary("PROGDIR:gtlayout.library",36)))
  3796.     {
  3797.         if(!(GTLayoutBase = SafeOpenLibrary("gtlayout.library",36)))
  3798.         {
  3799.             STATIC UBYTE LocalBuffer[256];
  3800.  
  3801.             STRPTR String;
  3802.             BOOL GotIt;
  3803.             LONG i;
  3804.  
  3805.             String = LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_GTLAYOUT_LIBRARY_TXT);
  3806.  
  3807.             GotIt = FALSE;
  3808.  
  3809.             for(i = 0 ; i < strlen(String) ; i++)
  3810.             {
  3811.                 if(String[i] == '%')
  3812.                 {
  3813.                     GotIt = TRUE;
  3814.                     break;
  3815.                 }
  3816.             }
  3817.  
  3818.             if(GotIt)
  3819.                 LimitedSPrintf(sizeof(LocalBuffer),LocalBuffer,String,36);
  3820.             else
  3821.                 LimitedSPrintf(sizeof(LocalBuffer),LocalBuffer,"%s (V%ld)",String,36);
  3822.  
  3823.             return(LocalBuffer);
  3824.         }
  3825.     }
  3826.  
  3827.     if(GetVar("termoldcycle",PathBuffer,sizeof(PathBuffer),NULL) >= 0 || GetVar("RussLeBar",PathBuffer,sizeof(PathBuffer),NULL) >= 0)
  3828.     {
  3829.         LONG Value;
  3830.  
  3831.         CycleType = CYCLE_KIND;
  3832.  
  3833.         if(StrToLong(PathBuffer,&Value) > 0)
  3834.         {
  3835.             if(!Value)
  3836.                 CycleType = POPUP_KIND;
  3837.         }
  3838.     }
  3839.     else
  3840.         CycleType = POPUP_KIND;
  3841.  
  3842.         /* Open OwnDevUnit.library, and allocate a signal bit if it opens. */
  3843.  
  3844.     if(OwnDevUnitBase = OpenLibrary(ODU_NAME,0))
  3845.     {
  3846.             /* If we can't get the signal, ignore the library */
  3847.  
  3848.         if((OwnDevBit = AllocSignal(-1)) == -1)
  3849.         {
  3850.             CloseLibrary(OwnDevUnitBase);
  3851.             OwnDevUnitBase = NULL;
  3852.         }
  3853.     }
  3854.  
  3855.         /* Open workbench.library, don't complain if it fails. */
  3856.  
  3857.     WorkbenchBase = OpenLibrary("workbench.library",0);
  3858.  
  3859.         /* Open icon.library as well, don't complain if it fails either. */
  3860.  
  3861.     IconBase = OpenLibrary("icon.library",0);
  3862.  
  3863.         /* Try to open datatypes.library, just for the fun of it. */
  3864.  
  3865.     DataTypesBase = OpenLibrary("datatypes.library",39);
  3866.  
  3867.     if(!(ConsoleRequest = (struct IOStdReq *)AllocVecPooled(sizeof(struct IOStdReq),MEMF_ANY|MEMF_CLEAR)))
  3868.         return(LocaleString(MSG_TERMINIT_FAILED_TO_ALLOCATE_CONSOLE_REQUEST_TXT));
  3869.  
  3870.     ConsoleRequest->io_Message.mn_Length = sizeof(struct IOStdReq);
  3871.  
  3872.     if(OpenDevice("console.device",CONU_LIBRARY,(struct IORequest *)ConsoleRequest,NULL))
  3873.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_CONSOLE_DEVICE_TXT));
  3874.  
  3875.     ConsoleDevice = ConsoleRequest->io_Device;
  3876.  
  3877.     if(!(FakeInputEvent = (struct InputEvent *)AllocVecPooled(sizeof(struct InputEvent),MEMF_ANY|MEMF_CLEAR)))
  3878.         return(LocaleString(MSG_TERMINIT_FAILED_TO_ALLOCATE_INPUTEVENT_TXT));
  3879.  
  3880.     FakeInputEvent->ie_Class = IECLASS_RAWKEY;
  3881.  
  3882.     if(!(MacroKeys = (struct MacroKeys *)AllocVecPooled(sizeof(struct MacroKeys),MEMF_ANY|MEMF_CLEAR)))
  3883.         return(LocaleString(MSG_TERMINIT_FAILED_TO_ALLOCATE_MACROKEYS_TXT));
  3884.  
  3885.     if(!(CursorKeys = (struct CursorKeys *)AllocVecPooled(sizeof(struct CursorKeys),MEMF_ANY|MEMF_CLEAR)))
  3886.         return(LocaleString(MSG_TERMINIT_FAILED_TO_ALLOCATE_CURSORKEYS_TXT));
  3887.  
  3888.         /* Create the job queue. */
  3889.  
  3890.     if(!(MainJobQueue = CreateJobQueue()))
  3891.         return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  3892.  
  3893.         /* Create the job nodes. */
  3894.  
  3895.     for(i = 0 ; JobInitTable[i].Name ; i++)
  3896.     {
  3897.         if(!(*JobInitTable[i].Node = CreateJob(JobInitTable[i].Name,JobInitTable[i].Type,JobInitTable[i].Function,NULL)))
  3898.             return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  3899.     }
  3900.  
  3901.         /* Create all generic lists. */
  3902.  
  3903.     for(i = GLIST_UPLOAD ; i < GLIST_COUNT ; i++)
  3904.     {
  3905.         if(!(GenericListTable[i] = CreateGenericList()))
  3906.             return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  3907.     }
  3908.  
  3909.     GenericListTable[GLIST_UPLOAD]->Notify = NotifyUploadQueue;
  3910.  
  3911.         /* Add extra assignments. */
  3912.  
  3913.     AddExtraAssignment("PROGDIR:Fonts","Fonts");
  3914.     AddExtraAssignment("Fonts","Fonts");
  3915.     AddExtraAssignment("PROGDIR:Libs","Libs");
  3916.     AddExtraAssignment("Libs","Libs");
  3917.  
  3918.         /* Set up the attention buffers. */
  3919.  
  3920.     if(!(AttentionBuffers[0] = (STRPTR)AllocVecPooled(SCAN_COUNT * ATTENTION_BUFFER_SIZE,MEMF_ANY|MEMF_CLEAR)))
  3921.         return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_SEQUENCE_ATTENTION_INFO_TXT));
  3922.  
  3923.     for(i = 1 ; i < SCAN_COUNT ; i++)
  3924.         AttentionBuffers[i] = &AttentionBuffers[i - 1][ATTENTION_BUFFER_SIZE];
  3925.  
  3926.         /* Obtain the default environment storage
  3927.          * path.
  3928.          */
  3929.  
  3930.     if(!ConfigPath)
  3931.     {
  3932.         ConfigPath = PathBuffer;
  3933.  
  3934.         if(!GetEnvDOS("TERMCONFIGPATH",PathBuffer,sizeof(PathBuffer)))
  3935.         {
  3936.             if(!GetEnvDOS("TERMPATH",PathBuffer,sizeof(PathBuffer)))
  3937.             {
  3938.                 APTR LastPtr;
  3939.                 BPTR FileLock;
  3940.  
  3941.                 strcpy(PathBuffer,"TERM:config");
  3942.  
  3943.                 DisableDOSRequesters(&LastPtr);
  3944.  
  3945.                 if(FileLock = Lock("TERM:",ACCESS_READ))
  3946.                     UnLock(FileLock);
  3947.                 else
  3948.                 {
  3949.                     FileLock = DupLock(ThisProcess->pr_HomeDir);
  3950.  
  3951.                         /* Create TERM: assignment referring to
  3952.                          * the directory `term' was loaded from.
  3953.                          */
  3954.  
  3955.                     if(!AssignLock("TERM",FileLock))
  3956.                         UnLock(FileLock);
  3957.                 }
  3958.  
  3959.                 if(!(FileLock = Lock(PathBuffer,ACCESS_READ)))
  3960.                     FileLock = CreateDir(PathBuffer);
  3961.  
  3962.                 if(FileLock)
  3963.                     UnLock(FileLock);
  3964.  
  3965.                 EnableDOSRequesters(LastPtr);
  3966.             }
  3967.         }
  3968.     }
  3969.     else
  3970.     {
  3971.         if(GetFileSize(ConfigPath))
  3972.         {
  3973.             STRPTR Index;
  3974.  
  3975.             strcpy(PathBuffer,ConfigPath);
  3976.  
  3977.             Index = PathPart(PathBuffer);
  3978.  
  3979.             *Index = 0;
  3980.  
  3981.             ConfigFileName = ConfigPath;
  3982.  
  3983.             ConfigPath = PathBuffer;
  3984.         }
  3985.     }
  3986.  
  3987.         /* Check for proper assignment path if necessary. */
  3988.  
  3989.     if(!Strnicmp(ConfigPath,"TERM:",5))
  3990.     {
  3991.         APTR OldPtr;
  3992.         BPTR DirLock;
  3993.  
  3994.             /* Block dos requesters. */
  3995.  
  3996.         DisableDOSRequesters(&OldPtr);
  3997.  
  3998.             /* Try to get a lock on `TERM:' assignment. */
  3999.  
  4000.         if(DirLock = Lock("TERM:",ACCESS_READ))
  4001.             UnLock(DirLock);
  4002.         else
  4003.         {
  4004.                 /* Clone current directory lock. */
  4005.  
  4006.             DirLock = DupLock(ThisProcess->pr_CurrentDir);
  4007.  
  4008.                 /* Create TERM: assignment referring to
  4009.                  * current directory.
  4010.                  */
  4011.  
  4012.             if(!AssignLock("TERM",DirLock))
  4013.                 UnLock(DirLock);
  4014.         }
  4015.  
  4016.         EnableDOSRequesters(OldPtr);
  4017.     }
  4018.  
  4019.         /* Create proper path names. */
  4020.  
  4021.     if(ConfigFileName)
  4022.     {
  4023.         if(!GetFileSize(ConfigFileName))
  4024.             ConfigFileName = NULL;
  4025.     }
  4026.  
  4027.     if(!ConfigFileName)
  4028.     {
  4029.         strcpy(LastConfig,ConfigPath);
  4030.  
  4031.         AddPart(LastConfig,"term.prefs",MAX_FILENAME_LENGTH);
  4032.     }
  4033.     else
  4034.         strcpy(LastConfig,ConfigFileName);
  4035.  
  4036.     strcpy(DefaultPubScreenName,"Workbench");
  4037.  
  4038.         /* Special measure */
  4039.  
  4040.     if(!LastPhone[0])
  4041.         ZapPhonebook = TRUE;
  4042.     else
  4043.         ZapPhonebook = FALSE;
  4044.  
  4045.         /* Create both configuration buffers. */
  4046.  
  4047.     if(!(Config = CreateConfiguration(TRUE)))
  4048.         return(LocaleString(MSG_TERMINIT_FAILED_TO_ALLOCATE_PRIMARY_CONFIG_TXT));
  4049.  
  4050.     if(!(PrivateConfig = CreateConfiguration(TRUE)))
  4051.         return(LocaleString(MSG_TERMINIT_FAILED_TO_ALLOCATE_SECONDARY_CONFIG_TXT));
  4052.  
  4053.     ResetConfig(Config,ConfigPath);
  4054.  
  4055.     if(ZapPhonebook)
  4056.         LastPhone[0] = 0;
  4057.  
  4058.         /* Read some more environment variables. */
  4059.  
  4060.     GetEnvDOS("EDITOR",Config->PathConfig->Editor,sizeof(Config->PathConfig->Editor));
  4061.  
  4062.         /* Look for the default configuration file. */
  4063.  
  4064.     if(!ReadConfig(LastConfig,Config))
  4065.     {
  4066.         ResetConfig(Config,ConfigPath);
  4067.  
  4068.         Initializing = TRUE;
  4069.  
  4070.         LoadColours = TRUE;
  4071.  
  4072.             /* Now we can safely assume that this is the */
  4073.             /* first invocation of this program on the */
  4074.             /* current setup */
  4075.  
  4076.         FirstInvocation = TRUE;
  4077.     }
  4078.     else
  4079.     {
  4080.         Current2DefaultPalette(Config);
  4081.  
  4082.         if(Config->ScreenConfig->ColourMode == COLOUR_AMIGA)
  4083.             Initializing = FALSE;
  4084.         else
  4085.             Initializing = TRUE;
  4086.     }
  4087.  
  4088.     if(UseNewDevice)
  4089.         strcpy(Config->SerialConfig->SerialDevice,NewDevice);
  4090.  
  4091.     if(UseNewUnit)
  4092.         Config->SerialConfig->UnitNumber = NewUnit;
  4093.  
  4094.     if(Config->MiscConfig->OpenFastMacroPanel)
  4095.         HadFastMacros = TRUE;
  4096.  
  4097.         /* Set up the phonebook file name */
  4098.  
  4099.     if(!LastPhone[0])
  4100.     {
  4101.         if(Config->PhonebookFileName[0])
  4102.             strcpy(LastPhone,Config->PhonebookFileName);
  4103.         else
  4104.         {
  4105.             strcpy(LastPhone,    Config->PathConfig->DefaultStorage);
  4106.             AddPart(LastPhone,    "phonebook.prefs",MAX_FILENAME_LENGTH);
  4107.         }
  4108.     }
  4109.  
  4110.         /* Load the keyboard macros. */
  4111.  
  4112.     strcpy(LastMacros,    Config->PathConfig->DefaultStorage);
  4113.     AddPart(LastMacros,    "functionkeys.prefs",MAX_FILENAME_LENGTH);
  4114.  
  4115.     if(Config->MacroFileName[0])
  4116.     {
  4117.         if(LoadMacros(Config->MacroFileName,MacroKeys))
  4118.             strcpy(LastMacros,Config->MacroFileName);
  4119.         else
  4120.         {
  4121.             if(!LoadMacros(LastMacros,MacroKeys))
  4122.                 ResetMacroKeys(MacroKeys);
  4123.         }
  4124.     }
  4125.     else
  4126.     {
  4127.         if(!LoadMacros(LastMacros,MacroKeys))
  4128.             ResetMacroKeys(MacroKeys);
  4129.     }
  4130.  
  4131.         /* Load the cursor keys. */
  4132.  
  4133.     strcpy(LastCursorKeys,    Config->PathConfig->DefaultStorage);
  4134.     AddPart(LastCursorKeys,    "cursorkeys.prefs",MAX_FILENAME_LENGTH);
  4135.  
  4136.     if(Config->CursorFileName[0])
  4137.     {
  4138.         if(ReadIFFData(Config->CursorFileName,CursorKeys,sizeof(struct CursorKeys),ID_KEYS))
  4139.             strcpy(LastCursorKeys,Config->CursorFileName);
  4140.         else
  4141.         {
  4142.             if(!ReadIFFData(LastCursorKeys,CursorKeys,sizeof(struct CursorKeys),ID_KEYS))
  4143.                 ResetCursorKeys(CursorKeys);
  4144.         }
  4145.     }
  4146.     else
  4147.     {
  4148.         if(!ReadIFFData(LastCursorKeys,CursorKeys,sizeof(struct CursorKeys),ID_KEYS))
  4149.             ResetCursorKeys(CursorKeys);
  4150.     }
  4151.  
  4152.         /* Load the sound settings. */
  4153.  
  4154.     strcpy(LastSound,    Config->PathConfig->DefaultStorage);
  4155.     AddPart(LastSound,    "sound.prefs",MAX_FILENAME_LENGTH);
  4156.  
  4157.     if(Config->SoundFileName[0])
  4158.     {
  4159.         if(ReadIFFData(Config->SoundFileName,&SoundConfig,sizeof(struct SoundConfig),ID_SOUN))
  4160.             strcpy(LastSound,Config->SoundFileName);
  4161.         else
  4162.         {
  4163.             if(!ReadIFFData(LastSound,&SoundConfig,sizeof(struct SoundConfig),ID_SOUN))
  4164.                 ResetSound(&SoundConfig);
  4165.         }
  4166.     }
  4167.     else
  4168.     {
  4169.         if(!ReadIFFData(LastSound,&SoundConfig,sizeof(struct SoundConfig),ID_SOUN))
  4170.             ResetSound(&SoundConfig);
  4171.     }
  4172.  
  4173.         /* Initialize the sound support routines. */
  4174.  
  4175.     SoundInit();
  4176.  
  4177.         /* Load the phone number pattern / rates settings. */
  4178.  
  4179.     strcpy(LastPattern,    Config->PathConfig->DefaultStorage);
  4180.     AddPart(LastPattern,    "rates.prefs",MAX_FILENAME_LENGTH);
  4181.  
  4182.     if(Config->AreaCodeFileName[0])
  4183.     {
  4184.         if(PatternList = LoadTimeDateList(Config->AreaCodeFileName,&ErrorCode))
  4185.             strcpy(LastPattern,Config->AreaCodeFileName);
  4186.         else
  4187.             PatternList = LoadTimeDateList(LastPattern,&ErrorCode);
  4188.     }
  4189.     else
  4190.         PatternList = LoadTimeDateList(LastPattern,&ErrorCode);
  4191.  
  4192.     if(!(GlobalPhoneHandle = CreatePhonebook(0,FALSE)))
  4193.         return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  4194.  
  4195.     if(!PatternList)
  4196.     {
  4197.         if(!(PatternList = CreateList()))
  4198.             return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  4199.     }
  4200.  
  4201.         /* Load the translation tables. */
  4202.  
  4203.     strcpy(LastTranslation,        Config->PathConfig->DefaultStorage);
  4204.     AddPart(LastTranslation,    "translation.prefs",MAX_FILENAME_LENGTH);
  4205.  
  4206.     if(Config->TranslationFileName[0])
  4207.     {
  4208.         if(LoadDefaultTranslationTables(Config->TranslationFileName))
  4209.             strcpy(LastTranslation,Config->TranslationFileName);
  4210.         else
  4211.             LoadDefaultTranslationTables(LastTranslation);
  4212.     }
  4213.     else
  4214.         LoadDefaultTranslationTables(LastTranslation);
  4215.  
  4216.         /* Load the fast! macro settings. */
  4217.  
  4218.     strcpy(LastFastMacros,        Config->PathConfig->DefaultStorage);
  4219.     AddPart(LastFastMacros,        "fastmacros.prefs",MAX_FILENAME_LENGTH);
  4220.  
  4221.     if(Config->FastMacroFileName[0])
  4222.     {
  4223.         if(LoadFastMacros(Config->FastMacroFileName,&FastMacroList))
  4224.             strcpy(LastFastMacros,Config->FastMacroFileName);
  4225.         else
  4226.             LoadFastMacros(LastFastMacros,&FastMacroList);
  4227.     }
  4228.     else
  4229.         LoadFastMacros(LastFastMacros,&FastMacroList);
  4230.  
  4231.     FastMacroCount = GetListSize(&FastMacroList);
  4232.  
  4233.         /* Load the speech settings. */
  4234.  
  4235.     strcpy(LastSpeech,    Config->PathConfig->DefaultStorage);
  4236.     AddPart(LastSpeech,    "speech.prefs",MAX_FILENAME_LENGTH);
  4237.  
  4238.     if(Config->SpeechFileName[0])
  4239.     {
  4240.         if(ReadIFFData(Config->SpeechFileName,&SpeechConfig,sizeof(struct SpeechConfig),ID_SPEK))
  4241.             strcpy(LastSpeech,Config->SpeechFileName);
  4242.         else
  4243.         {
  4244.             if(!ReadIFFData(LastSpeech,&SpeechConfig,sizeof(struct SpeechConfig),ID_SPEK))
  4245.                 ResetSpeechConfig(&SpeechConfig);
  4246.         }
  4247.     }
  4248.     else
  4249.     {
  4250.         if(!ReadIFFData(LastSpeech,&SpeechConfig,sizeof(struct SpeechConfig),ID_SPEK))
  4251.             ResetSpeechConfig(&SpeechConfig);
  4252.     }
  4253.  
  4254.         /* Load the hotkey settings. */
  4255.  
  4256.     strcpy(LastKeys,    Config->PathConfig->DefaultStorage);
  4257.     AddPart(LastKeys,    "hotkeys.prefs",MAX_FILENAME_LENGTH);
  4258.  
  4259.     if(Config->HotkeyFileName[0])
  4260.     {
  4261.         if(LoadHotkeys(Config->HotkeyFileName,&Hotkeys))
  4262.             strcpy(LastKeys,Config->HotkeyFileName);
  4263.         else
  4264.         {
  4265.             if(!LoadHotkeys(LastKeys,&Hotkeys))
  4266.                 ResetHotkeys(&Hotkeys);
  4267.         }
  4268.     }
  4269.     else
  4270.     {
  4271.         if(!LoadHotkeys(LastKeys,&Hotkeys))
  4272.             ResetHotkeys(&Hotkeys);
  4273.     }
  4274.  
  4275.         /* Load the trap settings. */
  4276.  
  4277.     strcpy(LastTraps,    Config->PathConfig->DefaultStorage);
  4278.     AddPart(LastTraps,    "trap.prefs",MAX_FILENAME_LENGTH);
  4279.  
  4280.     WatchTraps = TRUE;
  4281.  
  4282.     if(Config->TrapFileName[0])
  4283.     {
  4284.         if(LoadTraps(Config->TrapFileName,GenericListTable[GLIST_TRAP]))
  4285.             strcpy(LastTraps,Config->TrapFileName);
  4286.         else
  4287.         {
  4288.             if(!LoadTraps(LastTraps,GenericListTable[GLIST_TRAP]))
  4289.                 WatchTraps = FALSE;
  4290.         }
  4291.     }
  4292.     else
  4293.     {
  4294.         if(!LoadTraps(LastTraps,GenericListTable[GLIST_TRAP]))
  4295.             WatchTraps = FALSE;
  4296.     }
  4297.  
  4298.         /* Set up the text pacing controls. */
  4299.  
  4300.     SendSetup();
  4301.  
  4302.         /* Set up the capture parser. */
  4303.  
  4304.     if(!(ParserStuff = CreateParseContext()))
  4305.         return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  4306.  
  4307.     if(!CaptureParserInit())
  4308.         return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  4309.  
  4310.         /* Are we to freeze the text buffer? */
  4311.  
  4312.     if(!Config->CaptureConfig->BufferEnabled)
  4313.         BufferFrozen = TRUE;
  4314.  
  4315.     ConOutputUpdate();
  4316.  
  4317.         /* Initialize the data flow parser. */
  4318.  
  4319.     ResetDataFlowFilter();
  4320.  
  4321.         /* Set up parsing jump tables. */
  4322.  
  4323.     if(!(SpecialTable = (JUMP *)AllocVecPooled(256 * sizeof(JUMP),MEMF_CLEAR | MEMF_ANY)))
  4324.         return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  4325.  
  4326.     for(i = 0 ; i < sizeof(SpecialKeys) / sizeof(struct SpecialKey) ; i++)
  4327.         SpecialTable[SpecialKeys[i].Key] = (JUMP)SpecialKeys[i].Routine;
  4328.  
  4329.     if(!(AbortTable = (JUMP *)AllocVecPooled(256 * sizeof(JUMP),MEMF_ANY)))
  4330.         return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  4331.  
  4332.     for(i = 0 ; i < 256 ; i++)
  4333.     {
  4334.         switch(AbortMap[i])
  4335.         {
  4336.             case 0:
  4337.  
  4338.                 AbortTable[i] = (JUMP)ParseCode;
  4339.                 break;
  4340.  
  4341.             case 1:
  4342.  
  4343.                 AbortTable[i] = (JUMP)DoCancel;
  4344.                 break;
  4345.  
  4346.             case 2:
  4347.  
  4348.                 AbortTable[i] = (JUMP)DoNewEsc;
  4349.                 break;
  4350.  
  4351.             case 3:
  4352.  
  4353.                 AbortTable[i] = (JUMP)DoNewCsi;
  4354.                 break;
  4355.         }
  4356.     }
  4357.  
  4358.     if(!(TrapStuff = CreateParseContext()))
  4359.         return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  4360.  
  4361.         /* Create the special event queue. */
  4362.  
  4363.     if(!(SpecialQueue = CreateMsgQueue(NULL,0)))
  4364.         return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  4365.  
  4366.         /* Set up the serial driver. */
  4367.  
  4368.     if(!CreateSerial(LocalBuffer,sizeof(LocalBuffer)))
  4369.     {
  4370.         ShowRequest(NULL,LocaleString(MSG_GLOBAL_TERM_HAS_A_PROBLEM_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),LocalBuffer);
  4371.  
  4372.         DeleteSerial();
  4373.     }
  4374.  
  4375.         /* Get a signal bit. */
  4376.  
  4377.     if((CheckBit = AllocSignal(-1)) == -1)
  4378.         return(LocaleString(MSG_TERMINIT_FAILED_TO_GET_CHECK_SIGNAL_TXT));
  4379.  
  4380.         /* Add the global term port. */
  4381.  
  4382.     Forbid();
  4383.  
  4384.     if(!(TermPort = (struct TermPort *)FindPort(TERMPORTNAME)))
  4385.     {
  4386.         if(!(TermPort = (struct TermPort *)AllocVec(sizeof(struct TermPort) + sizeof(TERMPORTNAME),MEMF_PUBLIC|MEMF_CLEAR)))
  4387.             return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_GLOBAL_PORT_TXT));
  4388.         else
  4389.         {
  4390.             NewList(&TermPort->MsgPort.mp_MsgList);
  4391.             InitSemaphore(&TermPort->OpenSemaphore);
  4392.  
  4393.             TermPort->MsgPort.mp_Flags            = PA_IGNORE;
  4394.             TermPort->MsgPort.mp_Node.ln_Name    = (char *)(TermPort + 1);
  4395.  
  4396.             strcpy(TermPort->MsgPort.mp_Node.ln_Name,TERMPORTNAME);
  4397.  
  4398.             AddPort(&TermPort->MsgPort);
  4399.         }
  4400.     }
  4401.  
  4402.     Permit();
  4403.  
  4404.         /* Keep another term task from removing the port. */
  4405.  
  4406.     TermPort->HoldIt = TRUE;
  4407.  
  4408.         /* Install a new term process. */
  4409.  
  4410.     ObtainSemaphore(&TermPort->OpenSemaphore);
  4411.  
  4412.     TermPort->OpenCount++;
  4413.  
  4414.     TermPort->HoldIt = FALSE;
  4415.  
  4416.     TermID = TermPort->ID++;
  4417.  
  4418.     ReleaseSemaphore(&TermPort->OpenSemaphore);
  4419.  
  4420.         /* Set up the ID string. */
  4421.  
  4422.     if(TermID)
  4423.         LimitedSPrintf(sizeof(TermIDString),TermIDString,"TERM.%ld",TermID);
  4424.     else
  4425.         strcpy(TermIDString,"TERM");
  4426.  
  4427.     if(RexxPortName[0])
  4428.     {
  4429.         for(i = 0 ; i < strlen(RexxPortName) ; i++)
  4430.             RexxPortName[i] = ToUpper(RexxPortName[i]);
  4431.  
  4432.         if(FindPort(RexxPortName))
  4433.             RexxPortName[0] = 0;
  4434.     }
  4435.  
  4436.     if(!RexxPortName[0])
  4437.         strcpy(RexxPortName,TermIDString);
  4438.  
  4439.         /* Install the hotkey handler. */
  4440.  
  4441.     SetupCx();
  4442.  
  4443.         /* Allocate the first few lines for the display buffer. */
  4444.  
  4445.     if(!CreateBuffer())
  4446.         return(LocaleString(MSG_TERMINIT_FAILED_TO_ALLOCATE_VIEW_BUFFER_TXT));
  4447.  
  4448.     if(!(XprIO = (struct XPR_IO *)AllocVec(sizeof(struct XPR_IO),MEMF_ANY|MEMF_CLEAR)))
  4449.         return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_PROTOCOL_BUFFER_TXT));
  4450.  
  4451.         /* Set up the external emulation macro data. */
  4452.  
  4453.     if(!(XEM_MacroKeys = (struct XEmulatorMacroKey *)AllocVecPooled((2 + 10 * 4) * sizeof(struct XEmulatorMacroKey),MEMF_ANY|MEMF_CLEAR)))
  4454.         return(LocaleString(MSG_TERMINIT_FAILED_TO_ALLOCATE_MACRO_KEY_DATA_TXT));
  4455.  
  4456.         /* Set up the default transfer protocol */
  4457.  
  4458.     if(Config->TransferConfig->DefaultType == XFER_XPR)
  4459.         strcpy(LastXprLibrary,Config->TransferConfig->DefaultLibrary);
  4460.  
  4461.     ProtocolSetup(FALSE);
  4462.  
  4463.         /* Load a keymap file if required. */
  4464.  
  4465.     if(Config->TerminalConfig->KeyMapFileName[0])
  4466.         KeyMap = LoadKeyMap(Config->TerminalConfig->KeyMapFileName);
  4467.  
  4468.     if(!(TermRexxPort = (struct MsgPort *)CreateMsgPort()))
  4469.         return(LocaleString(MSG_GLOBAL_FAILED_TO_CREATE_MSGPORT_TXT));
  4470.  
  4471.         /* If rexxsyslib.library opens cleanly it's time for
  4472.          * us to create the background term Rexx server.
  4473.          */
  4474.  
  4475.     if(RexxSysBase = (struct RxsLib *)OpenLibrary(RXSNAME,0))
  4476.     {
  4477.             /* Create a background process handling the
  4478.              * rexx messages asynchronously.
  4479.              */
  4480.  
  4481.         if(!(RexxProcess = StartProcessWaitForHandshake("term Rexx Process",(TASKENTRY)RexxServer,
  4482.             NP_Priority,    5,
  4483.             NP_StackSize,    8000,
  4484.             NP_WindowPtr,    -1,
  4485.         TAG_END)))
  4486.             return(LocaleString(MSG_TERMINIT_UNABLE_TO_CREATE_AREXX_PROCESS_TXT));
  4487.     }
  4488.  
  4489.         /* Install the public screen name, assumes that the user
  4490.          * wants the window to be opened on the screen, rather than
  4491.          * opening a custom screen.
  4492.          */
  4493.  
  4494.     if(SomePubScreenName[0])
  4495.     {
  4496.         strcpy(Config->ScreenConfig->PubScreenName,SomePubScreenName);
  4497.  
  4498.         Config->ScreenConfig->Blinking        = FALSE;
  4499.         Config->ScreenConfig->UseWorkbench    = TRUE;
  4500.  
  4501.         SomePubScreenName[0] = 0;
  4502.     }
  4503.  
  4504.     CreateQueueProcess();
  4505.  
  4506.     AddRendezvousSemaphore(RexxPortName);
  4507.  
  4508.         /* Start the rates accounting */
  4509.  
  4510.     if(!CreateAccountant())
  4511.         return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  4512.  
  4513.     if(DoIconify)
  4514.         return(NULL);
  4515.     else
  4516.     {
  4517.             /* Create the whole display. */
  4518.  
  4519.         if(Result = CreateDisplay(FALSE,!KeepQuiet))
  4520.             return(Result);
  4521.         else
  4522.         {
  4523.             PubScreenStuff();
  4524.  
  4525.             UpdateJob(MainJobQueue,SerialCheckJob,SIG_CHECK);
  4526.             UpdateJob(MainJobQueue,QueueJob,SIG_QUEUE);
  4527.  
  4528.             if(OwnDevUnitBase)
  4529.                 UpdateJob(MainJobQueue,OwnDevUnitJob,SIG_OWNDEVUNIT);
  4530.  
  4531.             if(TermRexxPort)
  4532.                 UpdateJob(MainJobQueue,RexxJob,SIG_REXX);
  4533.  
  4534.             return(NULL);
  4535.         }
  4536.     }
  4537. }
  4538.